Xamarin.forms: [Bug] Switch control OnColor property breaks application

Created on 24 Feb 2020  路  5Comments  路  Source: xamarin/Xamarin.Forms

Description

Adding a switch control within a view with OnColor property breaks the app.

Steps to Reproduce

  1. Create a View with switch control and add OnColor property to this switch
  2. Navigate to this view
  3. Application breaks
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)

Expected Behavior

Switch OnColor when state is activated should show switch "on" state with defined color.

Actual Behavior

The application drops NullReferenceException and stop working.

Basic Information

  • Version with issue: 4.4.0.991640
  • Last known good version: 4.3
  • IDE: Visual Studio 2019
  • Platform Target Frameworks:

    • UWP: 18362

2 high regression in-progress UWP bug

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)

All 5 comments

@juaal12 Could you attach a repro sample?. I created an example with some tests but I can't reproduce the problem.
issue9694

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)

https://github.com/xamarin/Xamarin.Forms/blob/109b1d4a3edfc2dad8a63c54c6c80b41d9f48f6b/Xamarin.Forms.Platform.UAP/SwitchRenderer.cs#L116-L122

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

Was this page helpful?
0 / 5 - 0 ratings