My application has a switch set up in XAML and backend C# code that responds to the toggle event
If you are using MVVM - the simplest option to resolve this would be to use property setter to detect value update (as mentioned on this link). But this option doesn't work if you want to call awaitable methods here. Also, as a best practice it is recommended that properties shouldn't implement any time consuming operations.
bool _isToggled;
public bool IsToggled
{
get
{
return _isToggled;
}
set
{
_isToggled = value;
// Add your update code here
}
}
PropertyChanged
event in ViewModelThe next option would be to subscribe to PropertyChanged
event in your viewmodel and appropriately handle it. This allows you to define async handlers which in turn can await asynchronous methods.
// Subscribe to event while constructing/assigning viewmodel
// viewCellVM.PropertyChanged += CategoryVM_PropertyChanged;
async void ViewCellVM_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if(e.PropertyName == nameof(ViewCellVM.IsToggled))
{
//call update code here
//await DB.Update();
}
}
OnAppearing()
in ViewCellAnother option is to make sure that the Toggled
event handler is assigned after the BindingContext
is updated on the ViewCell
. So, I did a quick experiment to track the order Handle_Toggled
, OnBindingContextChanged
, OnAppearing
methods are called while loading the view. The order turned out to be:
Handle_Toggled
OnBindingContextChanged
OnAppearing
Hence, assigning the handler in OnAppearing
method (in ViewCell's code-behind) should also work for this case:
//XAML:
protected override void OnAppearing()
{
base.OnAppearing();
switchBtn.Toggled += Handle_Toggled;
Debug.WriteLine("OnAppearing");
}
protected override void OnDisappearing()
{
base.OnDisappearing();
switchBtn.Toggled -= Handle_Toggled;
Debug.WriteLine("OnDisappearing");
}
This way Handle_Toggled
will only be called when the user toggles the switch.