網站首頁 編程語言 正文
上一篇我們已經介紹了如何使用NuGet把MVVMLight應用到我們的WPF項目中。這篇我們來了解下一個基本的MVVMLight框架所必須的結構和運行模式。
MVVMLight安裝之后,我們可以看到簡易的框架布局,如上篇,生成了一個ViewModel文件夾,ViewModel層的內容都放在這邊,除了Main對象的ViewModel之外,還包含一個ViewModelLocator文件,
用來注入當前的ViewModel全局實例。
一、先來說說分層結構:
如圖:
1、View負責前端展示,與ViewModel進行數據和命令的交互。
2、ViewModel,負責前端視圖業務級別的邏輯結構組織,并將其反饋給前端。
3、Model,主要負責數據實體的結構處理,與ViewModel進行交互。
根據上述的分層,我們來進行編碼。
先建立一個完整三層結構的目錄,如圖,包含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); } } }
很簡單,僅僅是包含一個實體對象,這邊注意的的是那他繼承了一個父類:ObservableObject,這個父類的作用就是保證能夠檢測屬性是否被改變。
它實現了INotifyPropertyChanged接口,通過觸發PropertyChanged事件達到通知UI更改的目的;
所以我們在定義實體對象的時候,只需要調用RaisePropertyChanged(PropertyName)就可以進行屬性更改通知了。
所以實體里面定義的每個屬性都加上RaisePropertyChanged(PropertyName)的調用,就可以實現對UI的交互更新了。
2、寫一個VideModel,來負責跟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> /// 構造函數 /// </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接口的能力,
能夠通過觸發PropertyChanged事件達到通知View的目的;
構造函數中對 Welcome 屬性進行了實例化。
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,所以應該顯示Welcome對象下的Introduction屬性。
這時候的ViewModel和View是沒有任何關系的,所以我們在code-Behind的構造函數中寫上如下代碼:?
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 賦值給當前視圖的數據上下文。所以可以在當前視圖中使用ViewModel中所有的公開屬性和命令。
執行效果如下:
二、再來說說構造器:
如果使用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類。
實際上他就是一個很基本的視圖模型注入器。在構造器中把使用到的ViewModel統一注冊,并生成單一實例。
然后使用屬性把它暴露出來,每當我們訪問屬性的時候,就會返回相應的ViewModel實例。
/* 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 實例化 public MainViewModel Main { get { return ServiceLocator.Current.GetInstance<MainViewModel>(); } } #endregion public static void Cleanup() { // TODO Clear the ViewModels } }
注意的是,這邊把MVVMLight 自帶的SimpleIoc作為默認的服務提供者,它是個簡易的注入框架。
為了統一化,并且在設計的時候可以看到看到ViewModel的數據,這邊用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 實例化 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實例之后,我們就可以在相應的View中使用了 ,原本的
public WelcomeView() { InitializeComponent(); this.DataContext = new WelcomeViewModel(); }
中的 this.DataContext = new WelcomeViewModel();
可以去掉了,直接在WelcomeView中這樣寫:
DataContext="{Binding Source={StaticResource Locator},Path=Welcome}"
如下圖:
這樣做的好處,一個是綁定化相對于簡單粗暴的賦值方式,更合理。一個是在可視化窗口可以看到所綁定的數據,達到所見即所得的友好效果。
如下:
當我們改掉綁定到的數據,編譯之后就會立馬呈現:
服務端開發人員可以專心寫ViewModel的業務邏輯代碼,UI開發人員可以專注設計視圖了,
同樣 ViewModel可以綁定到不同的視圖上,所以從這邊就可以體現出他其中的三個重要特性:低耦合、可重用性、獨立開發。
大家有沒有發現ViewModelLocator 類中還有個 ClearnUp()方法,主要目的用來清除ViewModel實例的。
ViewModelBase繼承了GalaSoft.MvvmLight.ICleanup接口,并在自己的類中寫好了Cleanup()虛方法。所以我們在實例ViewModel類中可以重寫Cleanup()來達到清除當前實例的目的。
原文鏈接:https://www.cnblogs.com/wzh2010/p/6285990.html
相關推薦
- 2022-10-01 Iptables防火墻limit模塊擴展匹配規則詳解_安全相關
- 2023-04-13 react native 配置路徑別名alias
- 2022-06-02 基于Redis6.2.6版本部署Redis?Cluster集群的問題_Redis
- 2022-07-10 ENOENT讀取文件報錯(fs)
- 2022-05-26 Python入門必讀的if語句嵌套方法_python
- 2022-11-12 Python?Multinomial?Naive?Bayes多項貝葉斯模型實現原理介紹_python
- 2022-05-11 linq中的連接操作符_實用技巧
- 2022-11-14 Go語言Goroutinue和管道效率詳解_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支