MVVMLight項(xiàng)目Model?View結(jié)構(gòu)及全局視圖模型注入器
MVVM和MVVMLight框架介紹及在項(xiàng)目中的使用詳解
上一篇我們已經(jīng)介紹了如何使用NuGet把MVVMLight應(yīng)用到我們的WPF項(xiàng)目中。這篇我們來了解下一個基本的MVVMLight框架所必須的結(jié)構(gòu)和運(yùn)行模式。
MVVMLight安裝之后,我們可以看到簡易的框架布局,如上篇,生成了一個ViewModel文件夾,ViewModel層的內(nèi)容都放在這邊,除了Main對象的ViewModel之外,還包含一個ViewModelLocator文件,
用來注入當(dāng)前的ViewModel全局實(shí)例。
一、先來說說分層結(jié)構(gòu):
如圖:
1、View負(fù)責(zé)前端展示,與ViewModel進(jìn)行數(shù)據(jù)和命令的交互。
2、ViewModel,負(fù)責(zé)前端視圖業(yè)務(wù)級別的邏輯結(jié)構(gòu)組織,并將其反饋給前端。
3、Model,主要負(fù)責(zé)數(shù)據(jù)實(shí)體的結(jié)構(gòu)處理,與ViewModel進(jìn)行交互。
根據(jù)上述的分層,我們來進(jìn)行編碼。
先建立一個完整三層結(jié)構(gòu)的目錄,如圖,包含Model、View、ViewModel三層文件夾:
1、寫一個Model,代碼如下:
using GalaSoft.MvvmLight; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MVVMLightDemo.Model { public class WelcomeModel : ObservableObject { private String introduction; /// <summary> /// 歡迎詞 /// </summary> public String Introduction { get { return introduction; } set { introduction = value; RaisePropertyChanged(()=>Introduction); } } }
很簡單,僅僅是包含一個實(shí)體對象,這邊注意的的是那他繼承了一個父類:ObservableObject,這個父類的作用就是保證能夠檢測屬性是否被改變。
它實(shí)現(xiàn)了INotifyPropertyChanged接口,通過觸發(fā)PropertyChanged事件達(dá)到通知UI更改的目的;
所以我們在定義實(shí)體對象的時候,只需要調(diào)用RaisePropertyChanged(PropertyName)就可以進(jìn)行屬性更改通知了。
所以實(shí)體里面定義的每個屬性都加上RaisePropertyChanged(PropertyName)的調(diào)用,就可以實(shí)現(xiàn)對UI的交互更新了。
2、寫一個VideModel,來負(fù)責(zé)跟View的交互。
using GalaSoft.MvvmLight; using MVVMLightDemo.Model; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MVVMLightDemo.ViewModel { public class WelcomeViewModel:ViewModelBase { /// <summary> /// 構(gòu)造函數(shù) /// </summary> public WelcomeViewModel() { Welcome = new WelcomeModel() { Introduction = "Hello World!" }; } #region 屬性 private WelcomeModel welcome; /// <summary> /// 歡迎詞屬性 /// </summary> public WelcomeModel Welcome { get { return welcome; } set { welcome = value; RaisePropertyChanged(()=>Welcome); } } #endregion } }
也很簡單,包含了一個命名為Welcome的WelcomeModel屬性,繼承了ViewBaseModel父類,
ViewBaseModel同時繼承 ObservableObject類和ICleanup接口。所以他同樣有INotifyPropertyChanged接口的能力,
能夠通過觸發(fā)PropertyChanged事件達(dá)到通知View的目的;
構(gòu)造函數(shù)中對 Welcome 屬性進(jìn)行了實(shí)例化。
3、寫一個View,來顯示和交互ViewModel。
<Window x:Class="MVVMLightDemo.View.WelcomeView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="WelcomeView" Height="300" Width="300"> <Grid> <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" > <TextBlock Text="{Binding Welcome.Introduction}" FontSize="30" ></TextBlock> </StackPanel> </Grid> </Window>
TextBlock 綁定了 Welcome.Introduction,所以應(yīng)該顯示W(wǎng)elcome對象下的Introduction屬性。
這時候的ViewModel和View是沒有任何關(guān)系的,所以我們在code-Behind的構(gòu)造函數(shù)中寫上如下代碼:
using MVVMLightDemo.ViewModel; using System.Windows; namespace MVVMLightDemo.View { /// <summary> /// Interaction logic for WelcomeView.xaml /// </summary> public partial class WelcomeView : Window { public WelcomeView() { InitializeComponent(); this.DataContext = new WelcomeViewModel(); } }
把 WelcomeViewModel 賦值給當(dāng)前視圖的數(shù)據(jù)上下文。所以可以在當(dāng)前視圖中使用ViewModel中所有的公開屬性和命令。
執(zhí)行效果如下:
二、再來說說構(gòu)造器:
如果使用NuGet安裝的是完整的一個是MVVM Light 框架,而非 MVVM Light libraries only的時候,總是會帶上ViewModelLocator類,并且生成資源字典并加入到了全局資源中。
<Application x:Class="MVVMLightDemo.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="View/WelcomeView.xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" d1p1:Ignorable="d" xmlns:d1p1="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:vm="clr-namespace:MVVMLightDemo.ViewModel" > <Application.Resources> <ResourceDictionary> <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" /> </ResourceDictionary> </Application.Resources> </Application>
所以每次App初始化的時候,就會去初始化ViewModelLocator類。
實(shí)際上他就是一個很基本的視圖模型注入器。在構(gòu)造器中把使用到的ViewModel統(tǒng)一注冊,并生成單一實(shí)例。
然后使用屬性把它暴露出來,每當(dāng)我們訪問屬性的時候,就會返回相應(yīng)的ViewModel實(shí)例。
/* In App.xaml: <Application.Resources> <vm:ViewModelLocator xmlns:vm="clr-namespace:MVVMLightDemo" x:Key="Locator" /> </Application.Resources> In the View: DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}" You can also use Blend to do all this with the tool's support. See http://www.galasoft.ch/mvvm */ using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Ioc; using Microsoft.Practices.ServiceLocation; namespace MVVMLightDemo.ViewModel { /// <summary> /// This class contains static references to all the view models in the /// application and provides an entry point for the bindings. /// </summary> public class ViewModelLocator { /// <summary> /// Initializes a new instance of the ViewModelLocator class. /// </summary> public ViewModelLocator() { ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default); #region Code Example ////if (ViewModelBase.IsInDesignModeStatic) ////{ //// // Create design time view services and models //// SimpleIoc.Default.Register<IDataService, DesignDataService>(); ////} ////else ////{ //// // Create run time view services and models //// SimpleIoc.Default.Register<IDataService, DataService>(); ////} #endregion SimpleIoc.Default.Register<MainViewModel>(); } #region 實(shí)例化 public MainViewModel Main { get { return ServiceLocator.Current.GetInstance<MainViewModel>(); } } #endregion public static void Cleanup() { // TODO Clear the ViewModels } }
注意的是,這邊把MVVMLight 自帶的SimpleIoc作為默認(rèn)的服務(wù)提供者,它是個簡易的注入框架。
為了統(tǒng)一化,并且在設(shè)計(jì)的時候可以看到看到ViewModel的數(shù)據(jù),這邊用ServiceLocator 又將SimpleIoc包裹了一層。
上面我們寫了一個Hello World,這時候就可以用這種方式改裝了。
/* In App.xaml: <Application.Resources> <vm:ViewModelLocator xmlns:vm="clr-namespace:MVVMLightDemo" x:Key="Locator" /> </Application.Resources> In the View: DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}" You can also use Blend to do all this with the tool's support. See http://www.galasoft.ch/mvvm */ using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Ioc; using Microsoft.Practices.ServiceLocation; namespace MVVMLightDemo.ViewModel { /// <summary> /// This class contains static references to all the view models in the /// application and provides an entry point for the bindings. /// </summary> public class ViewModelLocator { /// <summary> /// Initializes a new instance of the ViewModelLocator class. /// </summary> public ViewModelLocator() { ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default); #region Code Example ////if (ViewModelBase.IsInDesignModeStatic) ////{ //// // Create design time view services and models //// SimpleIoc.Default.Register<IDataService, DesignDataService>(); ////} ////else ////{ //// // Create run time view services and models //// SimpleIoc.Default.Register<IDataService, DataService>(); ////} #endregion SimpleIoc.Default.Register<MainViewModel>(); SimpleIoc.Default.Register<WelcomeViewModel>(); } #region 實(shí)例化 public MainViewModel Main { get { return ServiceLocator.Current.GetInstance<MainViewModel>(); } } public WelcomeViewModel Welcome { get { return ServiceLocator.Current.GetInstance<WelcomeViewModel>(); } } #endregion public static void Cleanup() { // TODO Clear the ViewModels } }
注冊完WelcomeViewModel實(shí)例之后,我們就可以在相應(yīng)的View中使用了 ,原本的
public WelcomeView() { InitializeComponent(); this.DataContext = new WelcomeViewModel(); }
中的 this.DataContext = new WelcomeViewModel();
可以去掉了,直接在WelcomeView中這樣寫:
DataContext="{Binding Source={StaticResource Locator},Path=Welcome}"
如下圖:
這樣做的好處,一個是綁定化相對于簡單粗暴的賦值方式,更合理。一個是在可視化窗口可以看到所綁定的數(shù)據(jù),達(dá)到所見即所得的友好效果。
如下:
當(dāng)我們改掉綁定到的數(shù)據(jù),編譯之后就會立馬呈現(xiàn):
服務(wù)端開發(fā)人員可以專心寫ViewModel的業(yè)務(wù)邏輯代碼,UI開發(fā)人員可以專注設(shè)計(jì)視圖了,
同樣 ViewModel可以綁定到不同的視圖上,所以從這邊就可以體現(xiàn)出他其中的三個重要特性:低耦合、可重用性、獨(dú)立開發(fā)。
大家有沒有發(fā)現(xiàn)ViewModelLocator 類中還有個 ClearnUp()方法,主要目的用來清除ViewModel實(shí)例的。
ViewModelBase繼承了GalaSoft.MvvmLight.ICleanup接口,并在自己的類中寫好了Cleanup()虛方法。所以我們在實(shí)例ViewModel類中可以重寫Cleanup()來達(dá)到清除當(dāng)前實(shí)例的目的。
以上就是MVVMLight 之Model View結(jié)構(gòu)及全局視圖模型注入器的詳細(xì)內(nèi)容,更多關(guān)于ViewModel 結(jié)構(gòu)及全局視圖模型注入器的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android重寫TextView實(shí)現(xiàn)文字整齊排版的方法(附demo源碼下載)
這篇文章主要介紹了Android重寫TextView實(shí)現(xiàn)文字整齊排版的方法,結(jié)合實(shí)例形式分析了Android重寫TextView實(shí)現(xiàn)文字整齊排版的相關(guān)技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2016-02-02Android系列---JSON數(shù)據(jù)解析的實(shí)例
JSON(JavaScript Object Notation)和XML,并稱為客戶端和服務(wù)端交互解決方案的倚天劍和屠龍刀,這篇文章主要介紹了Android系列---JSON數(shù)據(jù)解析的實(shí)例,有興趣的可以了解一下。2016-11-11Android View源碼解讀 DecorView與ViewRootImpl淺談
這篇文章主要解讀了Android View源碼,為大家詳細(xì)介紹DecorView與ViewRootImpl,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02Android仿淘寶頭條向上滾動廣告條ViewFlipper
這篇文章主要為大家詳細(xì)介紹了Android仿淘寶頭條向上滾動廣告條ViewFlipper,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05Android實(shí)現(xiàn)多維商品屬性SKU選擇
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)多維商品屬性SKU選擇,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-10-10Android自定義View實(shí)現(xiàn)抖音飄動紅心效果
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)抖音飄動紅心效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-05-05