简体   繁体   English

如何在 UWP 中切换 MVVM

[英]How to MVVM toggleswitch in UWP

How to correctly setup toggleswitch with command?如何使用命令正确设置切换开关? I am using behavior to launch command using Toggled event.我正在使用行为来使用 Toggled 事件启动命令。 However I have problem when user changed state of switch, but my model rejects it.但是,当用户更改开关的 state 时,我遇到了问题,但我的 model 拒绝了它。 I do not know how to correctly program it, so view rejects update if it was rejected by model.我不知道如何正确编程,所以如果被 model 拒绝,视图将拒绝更新。

Here are my attempts to do it (INotifyPropertyChanged details skipped for clarity):这是我的尝试(为清楚起见,跳过了 INotifyPropertyChanged 的详细信息):

1. One way command 1.单向命令

class ToggleSwitchVm {
     public bool IsOn => Model.IsOn;
     public ICommand SwitchManipulated {get;}
}

When user manipulates ToggleSwitch state in UI changes, but it is not related to state in model. Also toggleswitch ignores PropertyChanged events, while it is calling command:当用户在 UI 更改中操作 ToggleSwitch state 时,它与 model 中的 state 无关。并且 toggleswitch 在调用命令时忽略 PropertyChanged 事件:

async void SwitchManipulatedCommand(bool? state) {
    if(!Model.SetNewState(state.Value)) { // if failed to set state raise PropertyChanged to refresh view
        RaisePropertyChanged(nameof(IsOn)); // this call is ignored :(
    }
}

2. Two way property? 2.双向属性?

class ToggleSwitchVm { 
 
    private void ModelOnPropertyChanged(object sender, PropertyChangedArgs args){
       if(args.PropertyName == nameof(Model.IsOn)){
          IsOn = Model.IsOn;
       }

    }


    private bool _isOn;
    public bool IsOn 
    { 
        get => _isOn;
        set {
                if(SetValue(ref _isOn, value))
                {  // true if model was manipulated
                    // View set value, or model set value?
                    if(!Model.SetState(value))
                    {
                         RaisePropertyChanged(nameof(IsON)); // ignored by view
                    }
                }
            }
    }
}

This is a mess, because both Model and View set IsOn property and there is no way to know which one set it.这是一团糟,因为 Model 和 View 都设置了 IsOn 属性,并且无法知道是哪一个设置了它。

How to MVVM toggleswitch in UWP如何在 UWP 中切换 MVVM

The ToggleSwitch IsOnProperty is DependencyProperty , that means it could use to bind data with two way model. And you have no need to process the IsOn proprty in command method. ToggleSwitch IsOnPropertyDependencyProperty ,这意味着它可以使用两种方式绑定数据 model。并且您无需在命令方法中处理 IsOn proprty。 Please refer the following code to implement the feature.请参考以下代码来实现该功能。

Xaml Code Xaml 代码

<Page.DataContext>
    <local:ViewModel />
</Page.DataContext>

<Grid>
    <ToggleSwitch
        Header="Toggle work"
        IsOn="{Binding IsOn, Mode=TwoWay}"
        OffContent="Do work"
        OnContent="Working"
        />
</Grid>

Code behind代码隐藏

public class ViewModel: INotifyPropertyChanged
{
    private bool _isOn;
    public bool IsOn
    {
        get { return _isOn; }
        set { Set(ref _isOn, value); }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void Set<T>(ref T storage, T value, [CallerMemberName]string propertyName = null)
    {
        if (Equals(storage, value))
        {
            return;
        }

        storage = value;
        OnPropertyChanged(propertyName);
    }

    private void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM