0
votes

I have a xamarin forms switch , when it is toggled getting the following exception on ios version only:

{System.NullReferenceException: Object reference not set to an instance of an object
  at Xamarin.Forms.Platform.iOS.SwitchRenderer.OnElementToggled (System.Object sender, System.EventArgs e) [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.iOS\Renderers\SwitchRenderer.cs:78 
  at (wrapper delegate-invoke) System.EventHandler`1[Xamarin.Forms.ToggledEventArgs].invoke_void_object_TEventArgs(object,Xamarin.Forms.ToggledEventArgs)
  at Xamarin.Forms.Switch+<>c.<.cctor>b__21_0 (Xamarin.Forms.BindableObject bindable, System.Object oldValue, System.Object newValue) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\Switch.cs:14 
  at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindableObject+BindablePropertyContext context, System.Object value, System.Boolean currentlyApplying, Xamarin.Forms.Internals.SetValueFlags attributes, System.Boolean silent) [0x00120] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:463 
  at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.Internals.SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x00173] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:397 
  at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.Internals.SetValueFlags attributes) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:343 
  at Xamarin.Forms.Element.SetValueFromRenderer (Xamarin.Forms.BindableProperty property, System.Object value) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\Element.cs:250 
  at Xamarin.Forms.Element.Xamarin.Forms.IElementController.SetValueFromRenderer (Xamarin.Forms.BindableProperty property, System.Object value) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\Element.cs:246 
  at Xamarin.Forms.Platform.iOS.SwitchRenderer.OnControlValueChanged (System.Object sender, System.EventArgs e) [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.iOS\Renderers\SwitchRenderer.cs:73 
  at UIKit.UIControlEventProxy.Activated () [0x00004] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.18.2.1/src/Xamarin.iOS/UIKit/UIControl.cs:38 
  at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr)
  at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.18.2.1/src/Xamarin.iOS/UIKit/UIApplication.cs:86 
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0000e] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.18.2.1/src/Xamarin.iOS/UIKit/UIApplication.cs:65 
  at WeCareMobility.iOS.Application.Main (System.String[] args) [0x00002] in /Users/user/Desktop/Vipin/Copiloto/Mobile/WeCareMobility/WeCareMobility.iOS/Main.cs:20 }

Xaml:

<Switch Grid.Row="0" Grid.Column="1" Margin="{OnPlatform Android='0,0,25,0',iOS='0,0,0,8'}" Scale="{OnPlatform iOS= '0.7'}" VerticalOptions="Start" HorizontalOptions="FillAndExpand" IsToggled="False" Toggled="Switch_Toggled" />

CS:

private void Switch_Toggled(object sender, ToggledEventArgs e)
{
            // Perform an action after examining e.Value
            if (e.Value)
            {
                App.Current.MainPage = new NavigationPage(new FlyoutPage());
                _weCareStorageService.SetToggleValue(true);
            }
   }

I do not have any renderer for switch. It is working fine for android.
Upgraded to latest xamarin forms stable version 4.7.0.1080.
Using VS2019 mac. iOS sdk version:13.18.2.1 .
Cleaned,rebuilt. Still crash occurs in ios. Need some help...

1
How are you creating the switch? Do you have some code? I just tried it and it seems to work fineGerald Versluis
@GeraldVersluis Its a xamarin.Forms switchVipin Krishna
Set a breakpoint in your Switch_Toggled I suspect there is a null value in there at some pointGerald Versluis
@GeraldVersluis: Yes i got it by breakpoint. Dont know much about this. Navigation was the issue. I changed navigation like this: Device.BeginInvokeOnMainThread(() => { App.Current.MainPage = new NavigationPage(new FlyoutPage()); }); Solved issue.Vipin Krishna

1 Answers

0
votes

Navigation was the issue.Changed navigation like this:
Device.BeginInvokeOnMainThread(() => { App.Current.MainPage = new NavigationPage(new FlyoutPage()); });
Solved issue.