Pro mvvm读书笔记mvvm中的VM

1、构建ViewModels设计模式

设计模式的其中一个目标就是抽象构造一个给出指定类型的对象或者实现指定类型的接口的过程。须要把类给客户端,让客户端去使用,可是要隐藏类是具体的实现细节。并发

1.1The Applicationapp

在View中有个一个App,实现Application这个文件能够用来管理ViewModels。mvvm

xaml:ide

<Application x:Class="MvvmWpfApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:viewModel="clrnamespace:
MvvmWpfApp.ViewModel;assembly=MvvmWpfApp.ViewModel"
Startup="Application_Startup">
<Application.Resources>
<viewModel:ApplicationViewModel x:Key="applicationViewModel" />
</Application.Resources>
</Application>this

cs:spa

public partial class App : Application
{
private ApplicationViewModel _appViewModel;
private void Application_Startup(object sender, StartupEventArgs e)
{
_appViewModel = Resources["applicationViewModel"] as ApplicationViewModel;
if(_appViewModel != null)
{
_appViewModel.Startup();
}
}
}线程

1.2The Main Window设计

namespace MvvmWpfApp { //经过ApplicationViewModel来获取一个MainViewModel //从而赋给MainWindow.DataContext;
    public partial class App :Application { ApplicationViewModel _appViewModel; private void Application_Startup(object sender, StartupEventArgs e) { _appViewModel = Resources["applicationViewModel"] as ApplicationViewModel; if (_appViewModel != null) { _appViewModel.Startup(); MainViewModel mainViewModel = _appViewModel.CreateMainViewModel(); MainWindow mainWindow = new MainWindow(); mainWindow.DataContext = mainViewModel; this.MainWindow = mainWindow; this.MainWindow.Show(); } } } }

1.3并发处理code

dispatchobject把system.object实例绑定到dispatcher上,意味着只有dispatcher创造的线程能够访问这个对象。wpf一般有两个线程,一个用于呈 现,一个用于处理UI和程序的代码。后者不止一个职责,在点击按钮事件代码里面System.Threading.Thread.Sleep(10000),若是在移动UI,那么UI是不会动的。由于线程处于休眠状态了。处理UI移动的线程和程序代码用的是同一个线程。一般若是model或者是ViewModel接受到的不是UI线程的事件,会出现InvalidOperationExpcetion.

2、避免在viewmodel中出现调用view中的类。

当使用mvvm时,可能会有种状况是,当一个点击菜单按钮,要弹出另外一个窗体,此时最好不要去直接new一个窗体而破坏mvvm的模式,能够使用接口:

 

public interface IFilePathProvider { string GetLoadPath(); string GetSavePath(); } public MainWindowViewModel(IFilePathProvider filePathFinder) { _filePathFinder = filePathFinder; } private void Load() { string loadFilePath = _filePathFinder.GetLoadPath(); if (loadFilePath != null) { // The user has selected a file to open
 } } private void Save() { string saveFilePath = _filePathFinder.GetSavePath(); if(saveFilePath != null) { // The user has selected a file to save
 } } public class FilePathProvider : IFilePathProvider { public string GetLoadPath() { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "XML files (*.xml)|*.xml"; string filePath = null; bool? dialogResult = ofd.ShowDialog(); if(dialogResult.HasValue && dialogResult.Value) { filePath = ofd.FileName; } return filePath; } public string GetSavePath() { SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "XML files (*.xml)|*.xml"; string filePath = null; bool? dialogResult = sfd.ShowDialog(); if (dialogResult.HasValue && dialogResult.Value) { filePath = sfd.FileName; } return filePath; } }
相关文章
相关标签/搜索