Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin.Forms.Switch sends Toggled event when value is updated

So I'm still debuting with Xamarin.Forms. So far so good, if I put aside a few pesky bugs I encountered. Here's the new guy. Maybe one of you could tell me if I'm doing something wrong.

Basically, I have a Xamarin.Forms.Switch on my interface, and I'm listening for changes in its state with the Toggled event. The doc says about this event: "Event that is raised when this Switch is toggled by the user."

Unfortunately, when I update the value of the switch programmatically, the event fires.

var mySwitch = new Switch ();
mySwitch.Toggled += (object sender, ToggledEventArgs e) => {
    Console.WriteLine ("Switch.Toggled event sent");
};
mySwitch.IsToggled = true;

Any way to prevent the event from firing / to know that it's not the user who triggered it?

like image 918
aspyct Avatar asked Oct 06 '15 17:10

aspyct


1 Answers

The behavior you're experiencing is correct: every time the IsToggled property changes, the switch will fire the Toggled event.

I'm not sure if the Xamarin.Forms documentation has been updated recently, but as of today it says this about the Toggled event:

Event that is raised when this Switch is toggled

Sample Code

Here is sample code that prevents the Toggled event from being handled when the Toggled event is not fired by the user

enter image description here

using System;

using Xamarin.Forms;

namespace SwitchToggle
{
    public class SwitchPage : ContentPage
    {
        public SwitchPage()
        {
            var mySwitch = new Switch
            {
                IsToggled = true
            };
            mySwitch.Toggled += HandleSwitchToggledByUser;

            var toggleButton = new Button
            {
                Text = "Toggle The Switch"
            };
            toggleButton.Clicked += (sender, e) =>
            {
                mySwitch.Toggled -= HandleSwitchToggledByUser;
                mySwitch.IsToggled = !mySwitch.IsToggled;
                mySwitch.Toggled += HandleSwitchToggledByUser;
            };

            var mainLayout = new RelativeLayout();

            Func<RelativeLayout, double> getSwitchWidth = (parent) => parent.Measure(mainLayout.Width, mainLayout.Height).Request.Width;
            Func<RelativeLayout, double> getToggleButtonWidth = (parent) => parent.Measure(mainLayout.Width, mainLayout.Height).Request.Width;

            mainLayout.Children.Add(mySwitch,
                Constraint.RelativeToParent((parent) => parent.Width / 2 - getSwitchWidth(parent) / 2),
                Constraint.RelativeToParent((parent) => parent.Height / 2 - mySwitch.Height / 2)
            );
            mainLayout.Children.Add(toggleButton,
                Constraint.RelativeToParent((parent) => parent.Width / 2 - getToggleButtonWidth(parent) / 2),
                Constraint.RelativeToView(mySwitch, (parent, view) => view.Y + view.Height + 10)
            );

            Content = mainLayout;

        }

        async void HandleSwitchToggledByUser(object sender, ToggledEventArgs e)
        {
            await DisplayAlert(
                "Switch Toggled By User",
                "",
                "OK"
            );
        }
    }

    public class App : Application
    {
        public App()
        {
            MainPage = new NavigationPage(new SwitchPage());
        }
    }
}
like image 195
Brandon Minnick Avatar answered Sep 18 '22 01:09

Brandon Minnick