如何在加载页面时停止触发开关上的切换事件? [英] How can I stop a Toggled Event on a switch from being fired as a page is loaded?

查看:106
本文介绍了如何在加载页面时停止触发开关上的切换事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序在XAML中设置了一个开关,并响应了切换事件的后端C#代码

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

<ViewCell x:Name="ss">
   <Switch x:Name="ssSwitch" Toggled="SsSwitch"   />
</ViewCell>

void SsSwitch(object sender, ToggledEventArgs e) {
    // Code to update database here
}

第一次加载页面时,我注意到SwSwitch Toggled事件被调用.

When the page first loads I notice that the SwSwitch Toggled event is called.

是否有某种方法可以阻止这种情况的发生,因为在启动时没有理由对数据库进行更新.

Is there some way that I can stop this happening as there is no reason to do an update of the database on start up.

推荐答案

选项1:在ViewModel中使用属性设置器

如果您使用的是MVVM,解决此问题的最简单方法是使用属性设置器来检测值更新(如该任何耗时的操作.

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

选项2:在ViewModel中使用PropertyChanged事件

下一个选项是预订视图模型中的PropertyChanged事件并对其进行适当处理.这使您可以定义异步处理程序,从而可以等待异步方法.

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

选项3:在ViewCell中覆盖OnAppearing()

另一个选择是确保在ViewCell上更新BindingContext之后分配Toggled事件处理程序.因此,我做了一个快速实验,以跟踪在加载视图时调用Handle_ToggledOnBindingContextChangedOnAppearing方法的顺序.结果是:

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

因此,OnAppearing 方法中分配处理程序(在ViewCell的背后代码中)也适用于这种情况:

Hence, assigning the handler in OnAppearing method (in ViewCell's code-behind) should also work for this case:

//XAML:<Switch x:Name="switchBtn" IsToggled="{Binding IsToggled}" />
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");
}

这种方式Handle_Toggled仅在用户切换开关时被调用.

This way Handle_Toggled will only be called when the user toggles the switch.

这篇关于如何在加载页面时停止触发开关上的切换事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆