XamarinCommunityToolkit
XamarinCommunityToolkit copied to clipboard
[Bug] XCT:StatusBarEffect.Color clashes with Rg:Plugins.Popup on iOS
Description
When replacing the Application.Current.MainPage
with a page that has xct:StatusBarEffect.Color
from a PopupPage
, an error with RootViewController
will occur on iOS. It was first occured to me when I was trying to reset the entire navigation stack, from a PopupPage, while I was navigating using Prism. _navigationService.NavigateAsync("/NavigationPage/MainPage");
. I had reproduced this issue in both environment, Xamarin.Forms independently, and Xamarin.Forms + Prism.Forms. By changing the Application.Current.MainPage
in a PopupPage, it will occur as well. This error will occur if I am trying to reset the navigation stack to a page with StatusBarEffect.
Exception
System.NullReferenceException: 'RootViewController'
Stack Trace
at Xamarin.CommunityToolkit.iOS.Effects.PlatformStatusBarEffect.UpdateStatusBarAppearance (UIKit.UIWindow window) [0x00010] in <b86a3dcb5c1f42eeafd9e295123844ec>:0
at Xamarin.CommunityToolkit.iOS.Effects.PlatformStatusBarEffect.SetColor (Xamarin.Forms.Color color) [0x00079] in <b86a3dcb5c1f42eeafd9e295123844ec>:0
at Xamarin.CommunityToolkit.iOS.Effects.PlatformStatusBarEffect.OnAttached () [0x0000b] in <b86a3dcb5c1f42eeafd9e295123844ec>:0
at Xamarin.Forms.Effect.SendAttached () [0x00009] in D:\a\_work\1\s\Xamarin.Forms.Core\Effect.cs:54
at Xamarin.Forms.RoutingEffect.SendAttached () [0x00000] in D:\a\_work\1\s\Xamarin.Forms.Core\RoutingEffect.cs:29
at Xamarin.Forms.Element.AttachEffect (Xamarin.Forms.Effect effect) [0x00045] in D:\a\_work\1\s\Xamarin.Forms.Core\Element.cs:513
at Xamarin.Forms.Element.set_EffectControlProvider (Xamarin.Forms.IEffectControlProvider value) [0x0007c] in D:\a\_work\1\s\Xamarin.Forms.Core\Element.cs:238
at Xamarin.Forms.Platform.iOS.EffectUtilities.RegisterEffectControlProvider (Xamarin.Forms.IEffectControlProvider self, Xamarin.Forms.IElementController oldElement, Xamarin.Forms.IElementController newElement) [0x0001a] in D:\a\_work\1\s\Xamarin.Forms.Platform.iOS\EffectUtilities.cs:20
at Xamarin.Forms.Platform.iOS.PageRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x00094] in D:\a\_work\1\s\Xamarin.Forms.Platform.iOS\Renderers\PageRenderer.cs:141
at Xamarin.Forms.Platform.iOS.Platform.CreateRenderer (Xamarin.Forms.VisualElement element) [0x00032] in D:\a\_work\1\s\Xamarin.Forms.Platform.iOS\Platform.cs:240
at Xamarin.Forms.Platform.iOS.NavigationRenderer.CreateViewControllerForPage (Xamarin.Forms.Page page) [0x00008] in D:\a\_work\1\s\Xamarin.Forms.Platform.iOS\Renderers\NavigationRenderer.cs:378
at Xamarin.Forms.Platform.iOS.NavigationRenderer.OnPushAsync (Xamarin.Forms.Page page, System.Boolean animated) [0x0001d] in D:\a\_work\1\s\Xamarin.Forms.Platform.iOS\Renderers\NavigationRenderer.cs:353
at Xamarin.Forms.Platform.iOS.NavigationRenderer.<ViewDidLoad>b__47_0 (Xamarin.Forms.Page p) [0x00024] in D:\a\_work\1\s\Xamarin.Forms.Platform.iOS\Renderers\NavigationRenderer.cs:239
at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1021
at Foundation.NSAsyncSynchronizationContextDispatcher.Apply () [0x00000] in /Users/builder/azdo/_work/2/s/xamarin-macios/src/Foundation/NSAction.cs:178
--- End of stack trace from previous location where exception was thrown ---
at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr)
at UIKit.UIApplication.Main (System.String[] args, System.Type principalClass, System.Type delegateClass) [0x0003b] in /Users/builder/azdo/_work/2/s/xamarin-macios/src/UIKit/UIApplication.cs:85
at XCTPopupStatusBarBugTest.iOS.Application.Main (System.String[] args) [0x00001] in C:\Users\DT005\source\repos\XCTPopupStatusBarBugTest\XCTPopupStatusBarBugTest\XCTPopupStatusBarBugTest.iOS\Main.cs:14
Link to Reproduction Sample
Steps to Reproduce
- Run the app.
- Click on "Open Popup Page"
- Click on "Reset Stack" OR "Back to Root using Command"
Expected Behavior
Navigation stack is reset and go to MainPage without any error.
Actual Behavior
Error occurs when stack is reset.
Basic Information
-
Version with issue:
- Xamarin.Forms
v5.0.0.2401
- Xamarin.CommunityToolkit
v2.0.2
- Rg.Plugin.Popup
v2.1.0
- Prism.DryIoc.Forms
v8.1.97
- Prism.Plugin.Popups
v8.0.76
- Xamarin.Forms
-
Last known good version:
-
IDE:
- Visual Studio 2022
-
Platform Target Frameworks:
- iOS: 15.4
-
Nuget Packages:
- Xamarin.Essentials
v1.7.3
- Xamarin.Essentials.Interfaces
v1.7.3
- Xamarin.Essentials
I had already create a bug report in Rg.Plugins.Popup repository as well.
@maxchu92 what is the exception that is thrown?
@maxchu92 what is the exception that is thrown?
System.NullReferenceException: 'RootViewController'
@maxchu92 I tried your sample, without prism and it fails when we try to get the RootViewController
in the image below you can see the error during my debug session.
I don't know too much about the Rg.Popup implementation, I just know it uses the PageRenderer
to present the popup, and maybe in that implementation, they didn't set the RootViewController
keeping it as null
which causes the error.
For reference, when I use the normal Xamarin.Forms.Page
the UIWindow
is a specific iOS object, as you can see in the image below.
I would say that's not on us because that works for normal Xamarin.Forms.Pages
. I'll keep this issue open for now, so let us know if some maintainer of Rg.Popups say something on your issue, I'm more than happy to work with them and fix it (:
@pictos I suspect that this is occurring because of changing the applications MainPage
property. I guess this is a rather uncommon scenario but as it is possible we probably should handle it safely. What danger do we have in not throwing the exception?
@bijington, yeah we can say that's not a good practice to set the MainPage
directly during the runtime and the dev should manipulate using the INavigation
methods... But for XF pages that works very well, so just curious to see if there's an easy fix for that.
What danger do we have in not throwing the exception?
Looking at the code the StatusBarColor will not be changed, but I don't know what could happen during runtime
@maxchu92 I tried your sample, without prism and it fails when we try to get the
RootViewController
in the image below you can see the error during my debug session.I don't know too much about the Rg.Popup implementation, I just know it uses the
PageRenderer
to present the popup, and maybe in that implementation, they didn't set theRootViewController
keeping it asnull
which causes the error.For reference, when I use the normal
Xamarin.Forms.Page
theUIWindow
is a specific iOS object, as you can see in the image below.I would say that's not on us because that works for normal
Xamarin.Forms.Pages
. I'll keep this issue open for now, so let us know if some maintainer of Rg.Popups say something on your issue, I'm more than happy to work with them and fix it (:
Understood. Really appreciate your time on checking this. Thank you.
I faced the same problem. If I use Rg.Plugin.Popup, then after the Popup has been shown and closed. If you open Popup again or try to go to another screen, the application will crash.
[0:] An error occurred: 'RootViewController'. Callstack: ' at Xamarin.CommunityToolkit.iOS.Effects.PlatformStatusBarEffect.UpdateStatusBarAppearance (UIKit.UIWindow window) <0x135263840 + 0x00032> in <e29db44a5724499bb6d2544e250eefd7>:0
at Xamarin.CommunityToolkit.iOS.Effects.PlatformStatusBarEffect.SetColor (Xamarin.Forms.Color color) <0x135263028 + 0x0010e> in <e29db44a5724499bb6d2544e250eefd7>:0
at Xamarin.CommunityToolkit.iOS.Effects.PlatformStatusBarEffect.OnAttached () <0x135253a38 + 0x00014> in <e29db44a5724499bb6d2544e250eefd7>:0
at Xamarin.Forms.Effect.SendAttached () [0x00009] in D:\a\1\s\Xamarin.Forms.Core\Effect.cs:54
at Xamarin.Forms.RoutingEffect.SendAttached () [0x00000] in D:\a\1\s\Xamarin.Forms.Core\RoutingEffect.cs:29
at Xamarin.Forms.Element.AttachEffect (Xamarin.Forms.Effect effect) [0x00045] in D:\a\1\s\Xamarin.Forms.Core\Element.cs:513
at Xamarin.Forms.Element.set_EffectControlProvider (Xamarin.Forms.IEffectControlProvider value) [0x0007c] in D:\a\1\s\Xamarin.Forms.Core\Element.cs:238
at Xamarin.Forms.Platform.iOS.EffectUtilities.RegisterEffectControlProvider (Xamarin.Forms.IEffectControlProvider self, Xamarin.Forms.IElementController oldElement, Xamarin.Forms.IElementController newElement) [0x0001a] in D:\a\1\s\Xamarin.Forms.Platform.iOS\EffectUtilities.cs:20
at Xamarin.Forms.Platform.iOS.PageRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x00094] in D:\a\1\s\Xamarin.Forms.Platform.iOS\Renderers\PageRenderer.cs:141
at Xamarin.Forms.Platform.iOS.Platform.CreateRenderer (Xamarin.Forms.VisualElement element) [0x00032] in D:\a\1\s\Xamarin.Forms.Platform.iOS\Platform.cs:240
at Xamarin.Forms.Platform.iOS.NavigationRenderer.CreateViewControllerForPage (Xamarin.Forms.Page page) [0x00008] in D:\a\1\s\Xamarin.Forms.Platform.iOS\Renderers\NavigationRenderer.cs:378
at Xamarin.Forms.Platform.iOS.NavigationRenderer.OnPushAsync (Xamarin.Forms.Page page, System.Boolean animated) [0x0001d] in D:\a\1\s\Xamarin.Forms.Platform.iOS\Renderers\NavigationRenderer.cs:353
at Xamarin.Forms.NavigationPage.PushAsyncInner (Xamarin.Forms.Page page, System.Boolean animated) [0x00084] in D:\a\1\s\Xamarin.Forms.Core\NavigationPage.cs:452
at Xamarin.Forms.NavigationPage.PushAsync (Xamarin.Forms.Page page, System.Boolean animated) [0x0014e] in D:\a\1\s\Xamarin.Forms.Core\NavigationPage.cs:265
at MCS.BLEUpdater.UI.ViewModels.MainViewModel.NavigateToSettings () [0x0002a] in D:\Projects\MCS\MCS.BLEUpdater\MCS.BLEUpdater\MCS.BLEUpdater\UI\ViewModels\MainViewModel.cs:95
at CommunityToolkit.Mvvm.Input.AsyncRelayCommand.AwaitAndThrowIfFailed (System.Threading.Tasks.Task executionTask) [0x00016] in /_/src/CommunityToolkit.Mvvm/Input/AsyncRelayCommand.cs:351
at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1021
at (wrapper delegate-invoke) <Module>.invoke_void_object(object)
at Foundation.NSAsyncSynchronizationContextDispatcher.Apply () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/16.4.0.18/src/Xamarin.iOS/Foundation/NSAction.cs:176
--- End of stack trace from previous location where exception was thrown ---
at (wrapper managed-to-native) UIKit.UIApplication.xamarin_UIApplicationMain(int,intptr,intptr,intptr,intptr&)
at UIKit.UIApplication.UIApplicationMain (System.Int32 argc, System.String[] argv, System.IntPtr principalClassName, System.IntPtr delegateClassName) [0x00008] in /Library/Frameworks/Xamarin.iOS.framework/Versions/16.4.0.18/src/Xamarin.iOS/UIKit/UIApplication.cs:58
at UIKit.UIApplication.Main (System.String[] args, System.Type principalClass, System.Type delegateClass) [0x0003b] in /Library/Frameworks/Xamarin.iOS.framework/Versions/16.4.0.18/src/Xamarin.iOS/UIKit/UIApplication.cs:94
at MCS.BLEUpdater.iOS.Application.Main (System.String[] args) [0x00001] in D:\Projects\MCS\MCS.BLEUpdater\MCS.BLEUpdater\MCS.BLEUpdater.iOS\Main.cs:12
at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&)
at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0006a] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/corlib/System.Reflection/RuntimeMethodInfo.cs:395 '
Работа приложения завершена.
I found the reason that causes the application to crash. If the application supports a dark theme and has styles, then this leads to the application crashing when calling Popup on iOS.
If I comment out the code below in the App.xaml file, then the application works fine.
<Style TargetType="NavigationPage">
<Setter Property="BarBackgroundColor" Value="{DynamicResource BarBackgroundColor}" />
<Setter Property="BarTextColor" Value="{DynamicResource BarTextColor}" />
<Setter Property="xct:StatusBarEffect.Color" Value="{DynamicResource BarBackgroundColor}" />
<Setter Property="xct:StatusBarEffect.Style" Value="{AppThemeBinding Dark={x:Static xct:StatusBarStyle.LightContent}, Light={x:Static xct:StatusBarStyle.DarkContent}}" />
</Style>
<Style
ApplyToDerivedTypes="True"
TargetType="{x:Type ContentPage}">
<Setter Property="BackgroundColor" Value="{DynamicResource AppBackgroundColor}" />
<Setter Property="xct:StatusBarEffect.Color" Value="{DynamicResource BarBackgroundColor}" />
<Setter Property="xct:StatusBarEffect.Style" Value="{AppThemeBinding Dark={x:Static xct:StatusBarStyle.LightContent}, Light={x:Static xct:StatusBarStyle.DarkContent}}" />
</Style>
Tested on iOS 17