Switch crashing UWP app post upgrade to XF 3.6

NeilUK79NeilUK79 Member ✭✭

Just upgraded my app to Forms v3.6 when testing the Windows / UWP app any page with a switch crashes the app with the error:

Exception: {System.ArgumentException: The parameter is incorrect.

element
at Windows.UI.Xaml.VisualStateManager.GetVisualStateGroups(FrameworkElement obj)
at Xamarin.Forms.Platform.UWP.SwitchRenderer.UpdateOnColor()
at Xamarin.Forms.Platform.UWP.SwitchRenderer.OnControlLoaded(Object sender, RoutedEventArgs e)}
Handled: false
Message: "System.ArgumentException: The parameter is incorrect.\r\n\r\nelement\r\n at Windows.UI.Xaml.VisualStateManager.GetVisualStateGroups(FrameworkElement obj)\r\n at Xamarin.Forms.Platform.UWP.SwitchRenderer.UpdateOnColor()\r\n at Xamarin.Forms.Platform.UWP.SwitchRenderer.OnControlLoaded(Object sender, RoutedEventArgs e)"
Native View: To inspect the native object, enable native code debugging

I removed all binding from the Swtich so its just in the Xaml any ideas?

Posts

  • CheongTekMunCheongTekMun USMember ✭✭

    Yes, I encountered the same problem. Reverting back to X.F 3.5 now.

  • NeilUK79NeilUK79 Member ✭✭

    I found if you move setting Binding Context to Appearing even rather than constructor for UWP only it works, hope it helps you

  • CheongTekMunCheongTekMun USMember ✭✭

    Yes, it works. If there is a custom renderer, the following is needed as well.
    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) {
    if (e.PropertyName != "OnColor") {
    base.OnElementPropertyChanged(sender, e);
    }
    }

  • SuperGURUSuperGURU Member

    @NeilUK79 : What does "set Binding Context to Appearing" mean? Maybe a short Code-Example, so i know what the fix is?
    Thanks!

  • NeilUK79NeilUK79 Member ✭✭

    Sorry in the Page Appearing event do a this.BindingContext=MyViewModel to get around this problem until its fixed in Forms.

  • Thibault_VellicusThibault_Vellicus CHUniversity ✭✭

    Setting BindingContext onAppearing didn't worked for me.
    Reverting X.F to 3.5 did the trick, but as usual: many hours of work lost just for UWP, because the X.F update wasn't correctly tested...

  • PeterJohnStoryPeterJohnStory NOMember
    edited March 17

    Anybody report this? I have exactly the same problem, my XAML page with a Switch in crashes exactly as NeilUK79 describes. Just as he says, I can remove the Switch and the crash goes away.
    Actually, my Switch has IsVisible set to False; the minimum I need to do in order to avoid the crash is to set IsVisible to True in the XAML!
    In reality I don't want the switch to be visible, so I then set IsVisible to false in the Toggled handler code for the Switch (which I know will be called since IsToggled is bound to something that I know will be set as the application gets to its proper initial state)
    Ugly, but it works.

  • JamesAlbertJamesAlbert USMember

    I found a bug report here and linked to this forum thread.
    [UWP]XF3.6 Exception with Switch on StackLayout with IsVisible="false" #5517
    github.com/xamarin/Xamarin.Forms/issues/5517

    I can confirm that the custom renderer work-around works, however I needed to override both OnElementChanged() and OnElementPropertyChanged() to avoid the call to UpdateOnColor(), which required some significant complexity because UpdateOnColor() gets attached to the ToggleSwitch Control's Loaded event in the base class.

Sign In or Register to comment.