Adding a switch control within a view with OnColor property breaks the app.
System.NullReferenceException: Object reference not set to an instance of an object.
at Xamarin.Forms.Platform.UWP.SwitchRenderer.UpdateOnColor()
at Xamarin.Forms.Platform.UWP.SwitchRenderer.OnControlLoaded(Object sender, RoutedEventArgs e)
Switch OnColor when state is activated should show switch "on" state with defined color.
The application drops NullReferenceException and stop working.
@juaal12 Could you attach a repro sample?. I created an example with some tests but I can't reproduce the problem.
The created repro sample (using Xamarin.Forms 4.4 and UWP Target Framework 18362):
Issue9694.zip
I get the same crash when updating from Xamarin Forms 4.4 to 4.5.
I can reproduce it by creating a new Xamarin.Forms project. Add a switch to main page
Compile + Run UWP on local machine.
VS Community Edition 2019 on Windows.
Xamarin.Essentials v1.5.0
Xamarin.Forms v4.5.0.356
NetStandard.Library v2.0.3
Microsoft.NetCORE.UniversalWindowsPlatform v6.2.9
Error message
System.NullReferenceException: Object reference not set to an instance of an object.
at Xamarin.Forms.Platform.UWP.SwitchRenderer.UpdateOnColor()
at Xamarin.Forms.Platform.UWP.SwitchRenderer.OnControlLoaded(Object sender, RoutedEventArgs e)
frame.Value
is a Windows.UI.Color
NOT a Brush
, therefore _originalOnHoverColor.Opacity
NREs on line 120.
Looking at generic.xaml one would expect it to be a Brush, but apparently that is only the case when referencing Microsoft.UI.Xaml 2.1.190606001. A recent change was made to reference 2.3.191211002 when targeting 16299 or later and this changes the type from Brush to Color and thus causes the problem.
In fact 2.2.190830001 and later all exhibit this behavior. This smells like an undocumented breaking change in Microsoft.UI.Xaml that is also rather counter intuitive given the inconsistencies in the type change and since folks tend to reference the sdk themes and not go hunting in Microsoft.UI.Xaml for whatever they are overriding. See microsoft/microsoft-ui-xaml#1223.
It would probably be better to make a copy of the template and modify it as needed rather than trying to adjust it at runtime to avoid issues like this.
This is still happening for me after upgrading from Xamarin Forms 4.1 to 4.5
Most helpful comment
I get the same crash when updating from Xamarin Forms 4.4 to 4.5.
I can reproduce it by creating a new Xamarin.Forms project. Add a switch to main page
Compile + Run UWP on local machine.
VS Community Edition 2019 on Windows.
Xamarin.Essentials v1.5.0
Xamarin.Forms v4.5.0.356
NetStandard.Library v2.0.3
Microsoft.NetCORE.UniversalWindowsPlatform v6.2.9
Error message
System.NullReferenceException: Object reference not set to an instance of an object.
at Xamarin.Forms.Platform.UWP.SwitchRenderer.UpdateOnColor()
at Xamarin.Forms.Platform.UWP.SwitchRenderer.OnControlLoaded(Object sender, RoutedEventArgs e)