C# ViewModel更改后如何正确更新模型?
假设我们有一个具有以下属性的模型(classC# ViewModel更改后如何正确更新模型?,c#,.net,wpf,user-interface,mvvm,C#,.net,Wpf,User Interface,Mvvm,假设我们有一个具有以下属性的模型(classModel) public string InputFileName { get { return m_InputFileName; } set { m_InputFileName = value; RaiseNotifyPropertyChanged("InputFileName"); } } 上面的模型实现了INotifyPropertyChanged接口,因此我们还有以下方法和事件
Model
)
public string InputFileName
{
get { return m_InputFileName; }
set
{
m_InputFileName = value;
RaiseNotifyPropertyChanged("InputFileName");
}
}
上面的模型实现了INotifyPropertyChanged
接口,因此我们还有以下方法和事件。下面的RaiseNotifyPropertyChanged
方法用于更新ViewModel
以下是实现ViewModel的类的主要部分
以下是实现该视图的类的主要部分:
MainWindow.xaml
<TextBox Name="inputfileTextBox"
Text="{Binding Path=InputFileStr, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
<Button Name="submitButton"
Content="Submit"
Command="{Binding SubmitCommand}"/>
上述实现工作正常:
- 视图和视图模型正确地相互更新李>
- 模型将正确更新ViewModel
InputFileStr
中添加以下调用:
m_Model.InputFileName = value;
但是,这种更新模型的解决方案会产生明显的意外影响:
m\u Model.InputFileName=value;
)Update
方法:这个方法应该在不使用模型属性的情况下更新模型
public void Update(string inputFileName) // this method does not notifies the ViewModel
{
m_InputFileName = inputFileName;
}
此替代解决方案是正确的解决方案还是有更好的解决方案?根据您的模型,您通常只需调用“保存”方法或类似方法。大多数模型(比如,数据库)不需要/不希望实时地将每一个更改都提供给它们 因此,一般来说,流程是:
PropertyChanged
通常是个坏主意。首先,听它一点也不好玩。相反,如果模型接收到新数据,则使用新数据向VM引发一个语义更清晰的事件
tldr;基本上,不用太担心保持模型和视图模型的同步。通常,模型根本不会保存当前状态的副本!即使是这样,只要在视图模型准备好“提交”更改时更新它,并通过正常事件通知视图模型对模型的外部更改
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new ViewModel();
}
}
m_Model.InputFileName = value;
public void Update(string inputFileName) // this method does not notifies the ViewModel
{
m_InputFileName = inputFileName;
}