Search code examples
c#xamlxamarin.androidtoggle

Using C# how can I return the value of a toggle Switch from xaml


I have two xaml toggles in separate files that I want to update simultaneously (if one is switched on the other should be too (and vice versa). My first switch in xaml is:

<Switch Grid.Column="1" x:Name="toggleSwitch1" IsToggled="true" Toggled="OnToggled"/> with the method

 void OnToggled(object sender, ToggledEventArgs e)
    {
        //updateConsentValueForCategory();
        if (toggleSwitch1.IsToggled)
        {
            Console.WriteLine("Toggled on");
        }
        else
        {
            Console.WriteLine("Toggled off");
        }
    }

Converting OnToggled() to a return type gives me an error for toggleSwitch1 saying an object reference is required because it is non-static. How can I pull the toggle value and update another xaml file in sync?


Solution

  • Using C# how can I return the value of a toggle Switch from xaml

    You can use several methods to achieve this .

    1.use event Toggled of Switch as you mentioned.

    You can refer to the following code:

    private void OnToggled(object sender, ToggledEventArgs e)
    {
        Switch mySwitch1 = (Switch)sender;
        if (mySwitch1.IsToggled)
        {
            Console.WriteLine("Toggled on" );
        }
        else
        {
            Console.WriteLine("Toggled off");
        }
    }
    

    2.Another method is to use the binding way.

    Just create a ViewModel for the current page(e.g.TestPage1) and create a field (e.g. SwithOne) for property IsToggled of Switch .

    Please refer to the following code :

    Create a ViewModel class(e.g. MyViewModel.cs) and create field SwithOne .Make the ViewModel implement interface INotifyPropertyChanged.

     public class MyViewModel: INotifyPropertyChanged
    {
        private bool swithOne;
        public bool SwithOne
        {
            set
            {
                if (swithOne != value)
                {
                    swithOne = value;
                    OnPropertyChanged("SwithOne");
                }
            }
            get
            {
                return swithOne;
            }
        }
    
    
        public MyViewModel()
        {
            SwithOne = true; // assign a value for `SwithOne `
        }
    
        bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
        {
            if (Object.Equals(storage, value))
                return false;
    
            storage = value;
            OnPropertyChanged(propertyName);
            return true;
        }
    
        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
    }
    

    TestPage1.xaml.cs.

    public partial class TestPage1 : ContentPage
    {
         MyViewModel myViewModel;
    
        public TestPage1()
        {
            InitializeComponent();
    
            myViewModel = new MyViewModel();
            BindingContext = myViewModel;
        }
    
        private async void Button_Clicked(object sender, EventArgs e)
        {
            await Navigation.PushModalAsync( new TestPage2(myViewModel.SwithOne));
        }
    
    }
    

    TestPage1.xaml

            <Switch x:Name="toggleSwitch1" IsToggled="{Binding SwithOne}"   HorizontalOptions="Center">
            </Switch>
            <Button  Text="navigate"  Clicked="Button_Clicked"/>