Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Switch IsToggled Toggled Issue.

n3tzn3tz Member ✭✭

Good afternoon everyone, im very very new in xamarin.
I have a Switch and a Picker

<Switch IsToggled="{Binding IsSelected}" Toggled="OnToggledEvent" Margin="0,0,40,0" Grid.Row="0" Grid.Column="2" />

When I choose a user from the picker, it triggers the IsToggled = "{Binding IsSelected}" event.
then load the user permissions and the switches are turned on or off

But when the switch is moved automatically, it triggers the Toggled = "OnToggledEvent" event. with that event I grant and remove the permissions
so when I choose a user from the picker, it loads me the permissions and at the same time grants or removes permissions without me pressing the switch

How can I make Toggled = "OnToggledEvent" only activate when I press the switch and not when it moves with the picker?
this is my code

` public class PermisosGenerales : INotifyPropertyChanged
{

    public string id { get; set; }
    public string Pagina { get; set; }

    public string Modulo { get; set; }
    public string Acceso { get; set; }


    private bool isSelected;
    public bool IsSelected
    {
        get { return isSelected; }
        set
        {
            isSelected = value;
         OnPropertyChanged(nameof(IsSelected));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string Acceso)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(Acceso));
    }


}`

PICKER CHANGE LOAD SWITCHES

` private async void Picker_Changed(object sender, EventArgs e)
{

            var selectedValue = PickerEstados.Items[PickerEstados.SelectedIndex];
            var uri = "api/Usuario?usuario=" + selectedValue;
            var request = new HttpRequestMessage();
            request.RequestUri = new Uri(uri);
            var client = new HttpClient();
            HttpResponseMessage response = await client.SendAsync(request);
            HttpContent content = response.Content;
            var json = await content.ReadAsStringAsync();
            var result = JsonConvert.DeserializeObject<List<PermisosGenerales>>(json);
            MyListView.ItemsSource = result;


            List<PermisosGenerales> itemSource = result.ToList();
            foreach (PermisosGenerales item1 in itemSource)
            {
                if (item1.Acceso == "SI")
                {
                    item1.IsSelected = true;
                }
                else
                {
                    item1.IsSelected = false;
                }
            }        
    }`

TOGGLED EVENT

` public async void OnToggledEvent(object sender, EventArgs args)
{
ViewCell cell = (sender as Switch).Parent.Parent as ViewCell;
PermisosGenerales model = cell.BindingContext as PermisosGenerales;

        if (PickerEstados.SelectedIndex != -1 && model.Acceso == "SI")
        {

           var selectedValue = PickerEstados.Items[PickerEstados.SelectedIndex];
            var client = new HttpClient();
            var uri = new Uri(string.Format("api/BorrarPermiso?Usuario=" + selectedValue + "&Pagina=" + model.Pagina));
            var request = new HttpRequestMessage(HttpMethod.Delete, uri);
            var response = await client.SendAsync(request);
           }


        else  (PickerEstados.SelectedIndex != -1 && model.Acceso == "NO")
        {

            var selectedValue = PickerEstados.Items[PickerEstados.SelectedIndex];
            UserDialogs.Instance.ShowLoading(title: "Aplicando cambios...");
            var client = new HttpClient();
            var uri = new Uri(string.Format("api/AgregarPermiso?Usuario=" + selectedValue + "&Pagina=" + model.Pagina));
            var request = new HttpRequestMessage(HttpMethod.Post, uri);
            var response = await client.SendAsync(request);
        }


}`
Tagged:

Best Answers

  • LandLuLandLu Member, Xamarin Team Xamurai
    Accepted Answer

    Try to define a new property in the model like:

    public class PermisosGenerales : INotifyPropertyChanged
    {
        ...
    
        public bool IsFromPicker { set; get; }
        ...
    }
    

    When you changed its state from code behind set it to true:

    IsFromPicker = true;
    if (item1.Acceso == "SI")
    {
        item1.IsSelected = true;
    }
    else
    {
        item1.IsSelected = false;
    }
    
    

    We could exclude this condition in the event:

    public async void OnToggledEvent(object sender, EventArgs args)
    {
        ViewCell cell = (sender as Switch).Parent.Parent as ViewCell;
        PermisosGenerales model = cell.BindingContext as PermisosGenerales;
        if (model.IsFromPicker == false)
        {
            // Do some stuff
        }
        model.IsFromPicker = false;
    }
    
  • LandLuLandLu Member, Xamarin Team Xamurai
    Accepted Answer

    Could you please accept the answer if your issue has been solved.

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai
    Accepted Answer

    Try to define a new property in the model like:

    public class PermisosGenerales : INotifyPropertyChanged
    {
        ...
    
        public bool IsFromPicker { set; get; }
        ...
    }
    

    When you changed its state from code behind set it to true:

    IsFromPicker = true;
    if (item1.Acceso == "SI")
    {
        item1.IsSelected = true;
    }
    else
    {
        item1.IsSelected = false;
    }
    
    

    We could exclude this condition in the event:

    public async void OnToggledEvent(object sender, EventArgs args)
    {
        ViewCell cell = (sender as Switch).Parent.Parent as ViewCell;
        PermisosGenerales model = cell.BindingContext as PermisosGenerales;
        if (model.IsFromPicker == false)
        {
            // Do some stuff
        }
        model.IsFromPicker = false;
    }
    
  • n3tzn3tz Member ✭✭

    Thank you very much for your answer, what you suggest is an excellent option
    the problem is that when i manually touch the switch, does nothing
    when I choose a user from the picker, the property is set to true : model.IsFromPicker = True;

    so

    public void OnToggledEvent(object sender, EventArgs args)
            {
    
            if (model.IsFromPicker == false)
             {
    
    does nothing when I press the switch
    
    }
    model.IsFromPicker = false
    
  • n3tzn3tz Member ✭✭
    edited September 3

    Solved.

    @LandLu YOU SAVE ME, Thanks.

  • LandLuLandLu Member, Xamarin Team Xamurai
    Accepted Answer

    Could you please accept the answer if your issue has been solved.

Sign In or Register to comment.