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); } }`
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; }
Could you please accept the answer if your issue has been solved.
Answers
Try to define a new property in the model like:
When you changed its state from code behind set it to true:
We could exclude this condition in the event:
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
Solved.
@LandLu YOU SAVE ME, Thanks.
Could you please accept the answer if your issue has been solved.