是的。那麼你可以在視圖模型一個模型對象,並把它保存在一個文本文件中的文本(或任何你想你的應用程序做的)
class ViewModelDefault : INotifyPropertyChanged
{
// Bound to your textbox
public string TextboxProperty { get; set;}
// Instantiate modellayer in viewmodel
private ModelClass _modelClass = new ModelClass();
// RelayCommand property -> bound to button on viewmodel
// Will execute method "ExecuteCommand" that contains a call to a method in the ModelClass
public ICommand ExecuteModelMethod
{
get {
RelayCommand relayCommand = new RelayCommand(ExecuteCommand);
return relayCommand;
}
}
// Method that the RelayCommand will execute.
private void ExecuteCommand()
{
_modelClass.SaveTextInTextfile(TextboxProperty);
}
...
}
在上面的代碼我做的怎麼樣,這可能是一個例子使用RelayCommand完成。
RelayCommand是一個使用Action和Func等委託的類。這意味着你可以傳遞一個方法到RelayCommand對象中並讓它執行它。
RelayCommand允許您通過代理將視圖層中的UI控件綁定到基本方法。
如果您想進一步研究該主題,請閱讀代表。 Delegates (C# Programming Guide)
謝謝。你的回答幫助了我很多。 – hoxplus