New empty Universal App project, adding "MVVM Light libraries Only (PCL) NuGet" package in Project in Windows 8.1 and Windows Phone 8.1
Create a new view model class in shared project
usingGalasoft.mvvmlight;usingGalaSoft.MvvmLight.Command;usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceUniversal_mvvmlight. viewmodel{ Public classMainvm:viewmodelbase {Private stringMtitle ="HelloWorld"; Public stringTitle {Get{returnMtitle;} Set{mtitle=value; raisePropertyChanged ("Title"); } } PrivateRelaycommand Mbuttonclick; PublicRelaycommand ButtonClick {Get { returnMbuttonclick?? (Mbuttonclick =NewRelaycommand (() = { This. Title ="Fuck the world"; })); } } }}MAINVM
To bind the view model directly in code behind without the view model locator
Public MainPage () { this. InitializeComponent (); This New MAINVM (); }
Next, Bind in XAML
<stackpanel orientation="Vertical"Verticalalignment="Center"Horizontalalignment="Center"> <textblock fontsize=" -"Foreground=" White"text="{Binding Title}"Verticalalignment="Center"Horizontalalignment="Center"></TextBlock> <button command="{Binding ButtonClick}"Content="Click Me"Fontsize=" -"></Button> </StackPanel>
English original link: http://dontcodetired.com/blog/post/Using-MVVM-Light-in-Universal-Windows-Apps.aspx
Use MVVM light in the Windows Universal app