How can I stop a Toggled Event on a switch from being fired as a page is loaded?

前端 未结 2 1475
悲&欢浪女
悲&欢浪女 2021-01-18 14:02

My application has a switch set up in XAML and backend C# code that responds to the toggle event


   

        
2条回答
  •  借酒劲吻你
    2021-01-18 14:43

    Option-1: Use property setter in ViewModel

    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
        }
    }
    

    Option-2: Use PropertyChanged event in ViewModel

    The 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();
        }
    }
    

    Option-3: Override OnAppearing() in ViewCell

    Another 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.

提交回复
热议问题