maui
maui copied to clipboard
Tabs within the shell do not execute override the OnAppearing method
Description
Only the override of the first Tab is intercepted MauiTest.zip
Steps to Reproduce
- Download the attached MauiTest project.
- Insert breakpoint in override OnAppearing into secondPage.cs
- Compile and run on an Android device
-click hamburger icon -click Test -click Second
Version with bug
6.0.400 (current)
Last version that worked well
6.0.312
Affected platforms
I was not able test on other platforms
Affected platform versions
Android 11 - Android 12
Did you find any workaround?
No response
Relevant log output
No response
verified repro on android, the breakpoint on OnAppearing can't be hit, on windows, it can be hit.
To generalize this (testing on Android API 31 emulator): Shell tab navigation seems to behave weirdly in several ways: Using the AppShell.xaml from the example:
<Tab Title="Test" >
<ShellContent Title="First"
ContentTemplate="{DataTemplate local:FirstPage}"
Route="FirstPage"/>
<ShellContent Title="Second"
ContentTemplate="{DataTemplate local:SecondPage}"
Route="SecondPage" />
</Tab>
<ShellContent Title="ThirdPage"
ContentTemplate="{DataTemplate local:ThirdPage}"
Route="ThirdPage"/>
- apart from
OnAppearing,OnNavigatedXYZmethods are also not triggered onSecondPage. - when going to
SecondPage, navigating to a different page on the main hierarchy likeThirdPageand coming back, theFirstPageis displayed again (the selection was persisted in Xamarin, and it would come back toSecondPage) - setting the
ContentPage.Titleproperty of theFirstPagein codebehind or associated ViewModel, this is applied it to all tabs (FirstPageandSecondPagethen have this new title at the top). Setting the ContentPage.Title property in SecondPage code-behind/viewmodel has no effect on the displayed Title.
In general, Shell seems to behave as if there is only the first page is the only one integrated into Shell at all...
Do we have an update on when this will be fixed? From what i can see, this is is really blocking some folks from doing the migration to MAUI.
Did you guys find any workaround?
No work around from us. I have spent over a week trying to do different things but non works. I will probably need to remove that and create a custom renderer. Am not sure why MAUI team is not trying to resolve it. I know of at least 100 case of it. No traction on it at all. Please let me know if you find an easier work around.
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.
I have the same problem. Content of a tab is not loading because OnAppearing method is not executed for this tab.
This is a major bug of huge importance and with great impact. Please work on it soon.
Sorry to ask again, but apart from the bot notifying this has been put in the backlog, why is there no activity from Microsoft here at all? In my opinion and as others have stated, this is a critical bug that prevents any stable use of shell for us, and this is still "in the backlog" half a year after MAUI release?
StackOverflow / Can a Shell TabBar have a tab without a content page? shows another use case where the inability to put code in OnAppearing for each page is a fundamental showstopper. No workaround.
Given that Maui docs present Shell as the primary means of Navigation, the inability to know when a Page is about to show is a very serious flaw for Maui.
My recommended workaround until this is fixed: DON'T USE SHELL, Use NavigationPage.
More work for the app coder, but you've got full control, using a simple navigation paradigm that has worked for years in Xamarin Forms.
Well, I'm glad that by finding this page I realized at least that I am not an idiot, as I was starting to suspect after more than an hour of testing my code. Bummer !
I am working on an accounting application, and this app works with objects (accounts, transfer, expenses, etc) which all have statuses (like "submitted", "accepted", "rejected", and so on). This is done on several pages feeding on the same list of objects and then filtering it. After, for instance, "Approving" an object on page "Approvals", I would like to see my object appearing on the page "Finalized" i.e. I would like to trigger a refresh of the Finalized page. Currently I populate this page by calling a "LoadDataAsync()" method in the constructor (in Finalized.xaml.cs) because calling this LoadDataAsync() method in OnAppearing() will never execute it. So that means that the data is populated once, in the beginning, and will never get refreshed. OnAppearing() does not trigger on this page because it only triggers on the first page in my Tab (which is the only one working properly).
By the way, do you know of any way of solving this, short of navigating to this stupid "Finalized" page and setting a bound parameter in its viewmodel to trigger the reload ? I do not want to navigate to this page just so I can refresh it ! Thank you !!!
Microsoft, please fix this !
Not a workaround, but pub/sub using WeakReferenceManager can be of help in certain situations: https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/messagingcenter?view=net-maui-7.0
This issue has been around since Xamarin.Forms (xamarin/Xamarin.Forms#3855)
This issue also causes rendering issues for the view attached in the shell tab as it is not initialized properly. For example, items added to an ObservableCollection rendered in a StackLayout with a BindableLayout won't be rendered on the view.
Repro: https://github.com/durandt/maui-issue1-android-tabs
Is there a workaround for this? It is surprising that a bug this big is not fixed yet.
Just found this one the hard way today, surprised it's not been addressed yet, bit of a show stopper.
Just found this one the hard way today, surprised it's not been addressed yet, bit of a show stopper.
I don't know what else to do but chime in to bump this every few weeks. Why is nothing happening here from Microsoft's side?
Please just tell us what you plan to do or some kind of rough timeline!
We are totally blocked from finalizing the migration of our B2B app -which relies on shell navigation baked into all viewmodels- to MAUI...
This is actually pretty bad for Microsoft to not fix it for this long. We literally stopped our migration and went back to the latest XF. If I get some time I will see if I can build out a control in Maui that can handle it.
On Wed, Jan 11, 2023, 12:59 AM Stephan Kieburg @.***> wrote:
Just found this one the hard way today, surprised it's not been addressed yet, bit of a show stopper.
I don't know what else to do but chime in to bump this every few weeks. Why is nothing happening here from Microsoft's side?
Please just tell us what you plan to do or some kind of rough timeline!
We are totally blocked from completing the migration of our B2B app -which relies on shell navigation naked into all viewmodels- to MAUI...
— Reply to this email directly, view it on GitHub https://github.com/dotnet/maui/issues/8102#issuecomment-1378314806, or unsubscribe https://github.com/notifications/unsubscribe-auth/A2MSCJHSGVR6SSVLZCDJ2Y3WRZK5FANCNFSM5Y6FXPNA . You are receiving this because you commented.Message ID: @.***>
Weekly bump until someone in official capacity deigns to respond. This is not a run-of-the-mill issue, it's a total showstopper for shell apps.
For now, please try this as a workaround.
https://github.com/PureWeen/ShanedlerSamples/tree/main/ShanedlerSamples/Library/Workarounds https://github.com/PureWeen/ShanedlerSamples/blob/main/ShanedlerSamples/MauiProgram.cs#L13
Working on a permanent fix now. Apologies for this one falling off the radar for so long 👎
Hi @PureWeen , thank you for working on this!
I have tried your work-around on my repro ( https://github.com/durandt/maui-issue1-android-tabs ), and the following works:
- Nested AppShell second (top-)tab's OnAppearing is called (within the first bottom-tab)
But the following is not working:
- Nested AppShell second (top-)tab's OnAppearing is not called within the subsequent bottom-tabs Put it in other words:
<TabBar>
<Tab Title="Home" Icon="house">
<ShellContent Title="Home 1" ContentTemplate="{DataTemplate views:MainPage}" /><!-- Works -->
<ShellContent Title="Home 2" ContentTemplate="{DataTemplate views:MainPage}" /><!-- Works -->
</Tab>
<Tab Title="Other" Icon="information">
<ShellContent Title="Other 1" ContentTemplate="{DataTemplate views:MainPage}" /><!-- Works -->
<ShellContent Title="Other 2" ContentTemplate="{DataTemplate views:MainPage}" /><!-- DOES NOT work -->
</Tab>
</TabBar>
- Switching view using the bottom tabs (select second tab, then go back to first tab) AND THEN switching views with the top-tabs crashes with NullReferenceException in
ShellToolbarTrackerrWorkaround.OnPageChanged:
[mono-rt] [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object.
[mono-rt] at Microsoft.Maui.Controls.Platform.Compatibility.ShellToolbarTracker.UpdateLeftBarButtonItem() in D:\a\_work\1\s\src\Controls\src\Core\Compatibility\Handlers\Shell\Android\ShellToolbarTracker.cs:line 678
[mono-rt] at Microsoft.Maui.Controls.Platform.Compatibility.ShellToolbarTracker.OnPageChanged(Page oldPage, Page newPage) in D:\a\_work\1\s\src\Controls\src\Core\Compatibility\Handlers\Shell\Android\ShellToolbarTracker.cs:line 259
[mono-rt] at maui_issue1_android_tabs.Workarounds.HostExtensions.ShellWorkarounds.ShellToolbarTrackerrWorkaround.OnPageChanged(Page oldPage, Page newPage) in /Users/thidu06/projects/maui/maui-issue1-android-tabs/maui-issue1-android-tabs/Workarounds/AndroidShellWorkarounds.cs:line 157
[mono-rt] at Microsoft.Maui.Controls.Platform.Compatibility.ShellToolbarTracker.set_Page(Page value) in D:\a\_work\1\s\src\Controls\src\Core\Compatibility\Handlers\Shell\Android\ShellToolbarTracker.cs:line 120
[mono-rt] at maui_issue1_android_tabs.Workarounds.HostExtensions.ShellWorkarounds.ShellSectionRendererWorkaround.UpdateCurrentItem(ShellContent content) in /Users/thidu06/projects/maui/maui-issue1-android-tabs/maui-issue1-android-tabs/Workarounds/AndroidShellWorkarounds.cs:line 241
[mono-rt] at maui_issue1_android_tabs.Workarounds.HostExtensions.ShellWorkarounds.ShellSectionRendererWorkaround.OnPageSelected(Int32 position) in /Users/thidu06/projects/maui/maui-issue1-android-tabs/maui-issue1-android-tabs/Workarounds/AndroidShellWorkarounds.cs:line 207
[mono-rt] at Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRenderer.ViewPagerPageChanged.OnPageSelected(Int32 position) in D:\a\_work\1\s\src\Controls\src\Core\Compatibility\Handlers\Shell\Android\ShellSectionRenderer.cs:line 399
[mono-rt] at AndroidX.ViewPager2.Widget.ViewPager2.OnPageChangeCallback.n_OnPageSelected_I(IntPtr jnienv, IntPtr native__this, Int32 position) in C:\a\_work\3\s\generated\androidx.viewpager2.viewpager2\obj\Release\net6.0-android\generated\src\AndroidX.ViewPager2.Widget.ViewPager2.cs:line 318
[mono-rt] at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPI_V(_JniMarshal_PPI_V callback, IntPtr jnienv, IntPtr klazz, Int32 p0) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:line 55
Hi @PureWeen , thank you for working on this!
I have tried your work-around on my repro ( https://github.com/durandt/maui-issue1-android-tabs ), and the following works:
- Nested AppShell second (top-)tab's OnAppearing is called (within the first bottom-tab)
But the following is not working:
- Nested AppShell second (top-)tab's OnAppearing is not called within the subsequent bottom-tabs Put it in other words:
<TabBar> <Tab Title="Home" Icon="house"> <ShellContent Title="Home 1" ContentTemplate="{DataTemplate views:MainPage}" /><!-- Works --> <ShellContent Title="Home 2" ContentTemplate="{DataTemplate views:MainPage}" /><!-- Works --> </Tab> <Tab Title="Other" Icon="information"> <ShellContent Title="Other 1" ContentTemplate="{DataTemplate views:MainPage}" /><!-- Works --> <ShellContent Title="Other 2" ContentTemplate="{DataTemplate views:MainPage}" /><!-- DOES NOT work --> </Tab> </TabBar>
- Switching view using the bottom tabs (select second tab, then go back to first tab) AND THEN switching views with the top-tabs crashes with NullReferenceException in
ShellToolbarTrackerrWorkaround.OnPageChanged:[mono-rt] [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object. [mono-rt] at Microsoft.Maui.Controls.Platform.Compatibility.ShellToolbarTracker.UpdateLeftBarButtonItem() in D:\a\_work\1\s\src\Controls\src\Core\Compatibility\Handlers\Shell\Android\ShellToolbarTracker.cs:line 678 [mono-rt] at Microsoft.Maui.Controls.Platform.Compatibility.ShellToolbarTracker.OnPageChanged(Page oldPage, Page newPage) in D:\a\_work\1\s\src\Controls\src\Core\Compatibility\Handlers\Shell\Android\ShellToolbarTracker.cs:line 259 [mono-rt] at maui_issue1_android_tabs.Workarounds.HostExtensions.ShellWorkarounds.ShellToolbarTrackerrWorkaround.OnPageChanged(Page oldPage, Page newPage) in /Users/thidu06/projects/maui/maui-issue1-android-tabs/maui-issue1-android-tabs/Workarounds/AndroidShellWorkarounds.cs:line 157 [mono-rt] at Microsoft.Maui.Controls.Platform.Compatibility.ShellToolbarTracker.set_Page(Page value) in D:\a\_work\1\s\src\Controls\src\Core\Compatibility\Handlers\Shell\Android\ShellToolbarTracker.cs:line 120 [mono-rt] at maui_issue1_android_tabs.Workarounds.HostExtensions.ShellWorkarounds.ShellSectionRendererWorkaround.UpdateCurrentItem(ShellContent content) in /Users/thidu06/projects/maui/maui-issue1-android-tabs/maui-issue1-android-tabs/Workarounds/AndroidShellWorkarounds.cs:line 241 [mono-rt] at maui_issue1_android_tabs.Workarounds.HostExtensions.ShellWorkarounds.ShellSectionRendererWorkaround.OnPageSelected(Int32 position) in /Users/thidu06/projects/maui/maui-issue1-android-tabs/maui-issue1-android-tabs/Workarounds/AndroidShellWorkarounds.cs:line 207 [mono-rt] at Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRenderer.ViewPagerPageChanged.OnPageSelected(Int32 position) in D:\a\_work\1\s\src\Controls\src\Core\Compatibility\Handlers\Shell\Android\ShellSectionRenderer.cs:line 399 [mono-rt] at AndroidX.ViewPager2.Widget.ViewPager2.OnPageChangeCallback.n_OnPageSelected_I(IntPtr jnienv, IntPtr native__this, Int32 position) in C:\a\_work\3\s\generated\androidx.viewpager2.viewpager2\obj\Release\net6.0-android\generated\src\AndroidX.ViewPager2.Widget.ViewPager2.cs:line 318 [mono-rt] at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPI_V(_JniMarshal_PPI_V callback, IntPtr jnienv, IntPtr klazz, Int32 p0) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:line 55
Thank you for testing it out! Updated my workaround and tested against your sample and seem to be working alright now
@PureWeen Updated work-around works well, thank you Shane!
@durandt great to hear!!
This fix will be released with SR4. We were a little too late unfortunately to get it in for SR3.
Can confirm the the workaround works for me too. Thanks Shane!
This fix will be released with SR4. We were a little too late unfortunately to get it in for SR3.
Awesome, thank you! When will "SR4" be released? Is it the first Tuesday of the month? February or March?
Will the fix solve also the following two issues with OnAppearing? There is another issue with OnAppearing not executing (https://github.com/dotnet/maui/issues/9531) and another one that causes OnAppearing/OnDisappearing to be executed many times instead of just once (https://github.com/dotnet/maui/issues/10161). It would be worth testing the workaround also for them before SR4 is released.
Hey @PureWeen,
I was referred here from https://github.com/dotnet/maui/issues/9531. I installed your workaround nuget and added the builder command, but I'm still experiencing the issue with OnAppearing not running correctly - see my comments there (I was commenting there because that was the only OnAppearing issue I could see that was still open). I'm not using shell, so maybe your workaround only works with shell? I'm experiencing the issue in Windows on a Content/NavigationPage.
Thanks.