上一篇咱们简单对MvvmLight作了介绍。罗列了三个DLL中,各个命名空间下主要类的定义及大体做用。由于只是范范的概论,对于从未接触过MvvmLight的萌新来讲,根本就是在晃点他们。不过万事开头难么,本篇则会以Hello World般的简单例子,来给萌新们当头一击,教会他们使用MvvmLight最最基础的部分。html
首先仍是动手练习,打开免费又强大的Visual Studio 2015 Community,建立一个WPF Application。不建立Win10的Universal App是由于MvvmLight V5.2还不能给Universal App自动添加ViewModel等代码(咱们下次本身加)。不使用8.1 Runtime App是由于我没在本身电脑上装8.1的SDK ^o^。express
新建的WPF Application是一个简单至极的空项目,仅有App.xaml和MainWindow.xaml两个文件。XAML文件空空如也。框架
而后咱们经过NuGet添加MvvmLight的类库,完成以后多出ViewModel文件夹,包含如下两个文件:ide
MainViewModel.csspa
ViewModelLocator.cscode
另外App.xaml里将ViewModelLocator做为资源添加全局的Application.Resources里:orm
<Application.Resources> <ResourceDictionary> <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" xmlns:vm="clr-namespace:HelloMvvmLight.ViewModel" /> </ResourceDictionary> </Application.Resources>
很是遗憾没有像Windows Phone工程那样贴心的在MainWindow里添加对MainViewModel的DataContext绑定,心情很糟糕的咱们只有本身来了,顺便给MainViewModel里加上HelloWord的字样呗,完成后运行以下图:xml
下面咱们来大体讲解MvvmLight在程序中起到的做用。htm
MainWindow对应的ViewModel是MainViewModel,一般咱们是在MainWindow的XAML或cs文件里new一个ViewModel的实例,赋值给DataContext。但在MvvmLight中,建立实例的工做,交给了ViewModelLocator这个类。blog
在类ViewModelLocator里,咱们注册了MainViewModel,并经过属性Main来获取实例。
public class ViewModelLocator { /// <summary> /// Initializes a new instance of the ViewModelLocator class. /// </summary> public ViewModelLocator() { ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default); SimpleIoc.Default.Register<MainViewModel>(); } public MainViewModel Main { get { return ServiceLocator.Current.GetInstance<MainViewModel>(); } } public static void Cleanup() { // TODO Clear the ViewModels } }
以前提到安装MvvmLight库时App.xaml添加了ViewModelLocator实例的资源,在XAML中以StaticResource key的形式获取,Binding到MainWindow的DataContext。
<Window.DataContext> <Binding Path="Main" Source="{StaticResource Locator}"></Binding> </Window.DataContext>
使用ViewModelLocator有啥好处呢?
首先View和ViewModel之间再也不直接引用,而是经过ViewModelLocator关联。
其次储存在ViewModelLocator里的ViewModel相似于单例的存在,能够在全局引用绑定。
同时避免了某些状况下频繁建立ViewModel,却未作好资源释放形成的内存泄漏。(这里并非说全部的ViewModel都必须放到ViewModelLocator)
下面咱们来看下Command是如何绑定的,通知PropertyChanged以及ViewModelBase类 。
咱们添加一个Button,而后经过Command来把文字修改成Hello MvvmLight。
ViewModel的代码:
public class MainViewModel : ViewModelBase { private string title; public string Title { get { return title; } set { Set(ref title , value); } } public ICommand ChangeTitleCommand { get; set; } /// <summary> /// Initializes a new instance of the MainViewModel class. /// </summary> public MainViewModel() { Title = "Hello World"; ChangeTitleCommand = new RelayCommand(ChangeTitle); } private void ChangeTitle() { Title = "Hello MvvmLight"; } }
MainWindow的XAML:
<Window x:Class="HelloMvvmLight.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:HelloMvvmLight" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Window.DataContext> <Binding Path="Main" Source="{StaticResource Locator}"></Binding> </Window.DataContext> <Grid> <Grid.RowDefinitions> <RowDefinition></RowDefinition> <RowDefinition></RowDefinition> </Grid.RowDefinitions> <TextBlock Text="{Binding Title}"></TextBlock> <Button Grid.Row="1" Command="{Binding ChangeTitleCommand}"></Button> </Grid> </Window>
MvvmLight很贴心的为咱们实现了RelayCommand类,该类继承自ICommand接口。直接在XAML里绑定就能够了。固然若是是没有提供Command属性的控件,就须要用到Blend来添加behavior了(能够期待后续篇章介绍)。
MvvmLight的ViewModelBase颇有意思,继承了INotifyPropertyChanged接口,并提供了一个Set方法来给属性赋值,简单理解就是不用本身在ViewModel实现INotifyPropertyChanged,而后在属性赋值时通知了。固然MvvmLight也提供了手动通知的方法:
protected virtual void RaisePropertyChanged([CallerMemberName] string propertyName = null); protected virtual void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression);
至此一个最简单的使用MvvmLight框架的程序已经完成了。由于是入门写的比较简单,请各位大牛轻踩。