ViewModel更改后如何正确更新模型?

enz*_*m83 5 .net c# wpf user-interface mvvm

假设我们有一个具有以下属性的Model(类Model).

public string InputFileName
{
    get { return m_InputFileName; }
    set
    {
        m_InputFileName = value;
        RaiseNotifyPropertyChanged("InputFileName");
    }
}
Run Code Online (Sandbox Code Playgroud)

上面的模型实现了INotifyPropertyChanged接口,所以我们还有以下方法和以下事件.以下RaiseNotifyPropertyChanged方法用于更新ViewModel.

#region INotifyPropertyChanged Implementation

private void RaiseNotifyPropertyChanged(string property)
{
    var handler = PropertyChanged;
    if (handler != null)
    {
        handler(this, new PropertyChangedEventArgs(property));
    }
}

public event PropertyChangedEventHandler PropertyChanged;

#endregion
Run Code Online (Sandbox Code Playgroud)

以下是实现ViewModel的类的主要部分.

public class ViewModel : INotifyPropertyChanged
{
    #region Members

    private Model m_Model;

    private string m_InputFileStr;

    private readonly ICommand m_SubmitCommand;

    #endregion

    #region Constructors

    public ViewModel()
    {
        m_Model = new Model();
        m_Model.PropertyChanged += new PropertyChangedEventHandler(this.Model_PropertyChanged);

        m_InputFileStr = string.Empty;

        // ...
        // initialize m_SubmitCommand
    }

    #endregion

    // ...

    #region Properties

    public string InputFileStr
    {
        get { return m_InputFileStr; }
        set
        {
            if (value == m_InputFileStr) return;
            m_InputFileStr = value;
            OnPropertyChanged("InputFileStr");
            m_SubmitCommand.RaiseCanExecuteChanged();
        }
    }

    #endregion

    #region INotifyPropertyChanged Implementation

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    #endregion

    // This method is called when the model changes, so the Model notified the ViewModel.
    private void Model_PropertyChanged(object sender, PropertyChangedEventArgs args)
    {
        if (args.PropertyName == "InputFileName")
        {
            InputFileStr = m_Model.InputFileName;
        }
        else if (args.PropertyName == "OutputFileName")
        {
            OutputFileStr = m_Model.OutputFileName;
        }
        else if (args.PropertyName == "ReportText")
        {
            ReportTextStr = m_Model.ReportText;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

以下是实现View的类的主要部分:

MainWindow.xaml

<TextBox Name="inputfileTextBox"
         Text="{Binding Path=InputFileStr, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>

<Button Name="submitButton"
        Content="Submit"
        Command="{Binding SubmitCommand}"/>
Run Code Online (Sandbox Code Playgroud)

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new ViewModel();
    }
}
Run Code Online (Sandbox Code Playgroud)

以上实现正常:

  • View和ViewModel正确地相互更新;
  • 模型正确更新ViewModel.

为了使ViewModel能够更新Model,我想我会在InputFileStrViewModel 的set属性中添加以下调用:

m_Model.InputFileName = value;
Run Code Online (Sandbox Code Playgroud)

但是,这种更新模型的解决方案会导致明显的意外影响:

  1. 用户修改了View.
  2. ViewModel会自动修改.
  3. ViewModel更新Model(m_Model.InputFileName = value;).
  4. 模型已更新......
  5. ...所以它通知ViewModel有关更改

以上行为是正确的行为吗?我希望如果ViewModel更新模型,那么模型不必重新通知ViewModel有关相同的更改......作为替代解决方案我认为我会Update向模型中添加一个方法:此方法应该更新不使用模型属性的模型.

public void Update(string inputFileName)   // this method does not notifies the ViewModel
{
    m_InputFileName = inputFileName;
}
Run Code Online (Sandbox Code Playgroud)

这种替代解决方案是正确的解决方案还是更好的解决方案?

Bra*_*NET 6

根据您的模型,您通常只需调用"保存"方法或类似方法.大多数模型(比如数据库)不需要/希望实时给予它们每一个变化.

所以一般来说,流程将是:

  1. 用户调用"保存"操作
  2. 视图模型将此作为命令接收
  3. 视图模型使用新数据在模型上调用"保存"操作

如果您的DTO对象在模型和视图模型之间共享,则甚至不必担心同步.否则,这是同步它们的好时机.

在类似的说明中,PropertyChanged模型类中使用通常是个坏主意.对于初学者来说,听起来并不好玩.相反,如果模型接收到新数据,则使用新数据向VM引发更加语义清晰的事件.

tldr ; 基本上,不要太担心保持模型和视图模型同步.通常,模型根本不会保留当前状态的副本!即使它是,只需在视图模型准备"提交"更改时更新它,并通过正常事件通知视图模型对模型的外部更改.