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

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

问题描述

我的应用程序有一个在 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.

推荐答案

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

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

Option-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天全站免登陆