先科普一下:什么是WPF,請看下圖

微軟對於WPF技術的構想是很宏大的,可惜普及率不高,不過如果你要做Windows客戶端開發的話WPF技術還是值得一學的。
什么是MVVM模式#
簡單來說它是一種高級的UI設計模式。據我所知目前還運用在一些js框架中,比如AngularJS。其他的UI設計模式還包括MVC、MVP,個人覺得最強大的還是MVVM。
MVVM主體框架如下圖:

- The Model is the entity that represents the business concept; it can be anything from a simple customer entity to a complex stock trade entity .
- The View is the graphical control or set of controls responsible for rendering the Model data on screen .A View can be a WPF window, a Silverlight page, or just an XAML data template control .
- The ViewModel is the magic behind everything .The ViewModel contains the UI logic, the commands, the events, and a reference to the Model .
- In MVVM, the ViewModel is not in charge of updating the data displayed in the UI—thanks to the powerful data-binding engine provided by WPF and Silverlight, the ViewModel doesn’t need to do that .This is because the View is an observer of the ViewModel, so as soon as the ViewModel changes, the UI updates itself .For that to happen, the ViewModel must implement the INotifyPropertyChangedinterface and fire the PropertyChangedevent .
簡單翻譯一下(不全)
- The Model 代表業務邏輯的實體類,可以是一個簡單的顧客實體類,也可以是一個復雜的股票交易實體類。
- The View 代表一個用戶界面控件 ...
- The ViewModel 包括各種邏輯、命令、事件以及實體類的引用。
什么是MVVMFoundation#
MVVMFoundation是一個最簡單的MVVM框架,官方介紹如下:
MVVM Foundation is a library of classes that are very useful when building applications based on the Model-View-ViewModel philosophy. The library is small and concentrated on providing only the most indispensable tools needed by most MVVM application developers
MVVMFoundation包含四大模塊:
-
ObservableObject:這里相當於ViewModelBase的概念,每一個ViewModel繼承自該類,調用完成之后立即釋放,防止內存泄露。
-
RelayCommand接口:封裝command的聲明,包括execution執行邏輯,可選的can-execute邏輯等。外部只需要實例化並Binding就可以簡單使用。
-
Messenger:這里主要用在各種不同的ViewModel之間通信(比如相互關聯的ViewModel、主從ViewModel等),當然也可以擴展成ViewModel與View之間進行通信。
-
PropertyObserver:主要是對INotifyPropertyChanged.PropertyChanged進行封裝,可以通過其對某個對象的屬性變更注冊回調函數,當屬性變更時便觸發回調函數。
ObservableObject#
實現ViewModel中的屬性改變通知到綁定的控件的方法,相當於是所有Viewmodel的基類。
-
使用時調用OnPropertyChange方法,則后台數據變化即可通知界面刷新
public string UserName { get { return this.user.UserName; } set { this.user.UserName = value; OnPropertyChanged("UserName"); } }
-
屬性在View界面的綁定
<TextBox Text="{Binding UserName}" />
RelayCommand#
用於在ViewModel中定義View中綁定的命令,代替了以前Winform的Click事件。
-
在ViewModel中定義Command
public ICommand BrowseImageCommand { get { return new ICommand(BrowseImage); } } private void BrowseImage() { ... }
-
在View中的按鈕關聯此Command
<Button Content="瀏覽..." Command="{Binding BrowseImageCommand}"/>
Messenger#
可用於ViewModel之間的信息傳遞,可以用於ViewModel和View之間的信息傳遞。
-
定義信息傳輸類
public class ViewModelCommunication { static ViewModelCommunication() { Messaging = new Messenger(); } public static Messenger Messaging { get; set; } public static string DataIDInChanged { get { return "DataIDInChanged"; } } }
-
在需要通知的類中注冊要通知的信息
ViewModelCommunication.Messaging.Register(ViewModelCommunication.DataIDInChanged,(Action<string>)(param => SetLastSelectedDataID(param)));
-
當對應的消息出現時,通知已經注冊的類
ViewModelCommunication.Messaging.NotifyColleagues(ViewModelCommunication.DataIDInChanged, DataID.ToString());
PropertyObserver#
主要用於對對象的屬性監聽,屬性變更后可觸發已注冊的回調函數。
-
注冊要監聽對象的屬性及回調函數
PropertyObserver<UserInfoViewModel> userInfoAfterObserver; public MainWindowViewModel() { UserInfoBefore = new UserInfoViewModel(); userInfoAfterObserver = new PropertyObserver<UserInfoViewModel>(UserInfoAfter) .RegisterHandler(UserInfo => UserInfo.Age, this.AgeChangedCallback); }
-
實現回調函數
private void AgeChangedCallback(UserInfoViewModel userInfo) { MessageBox.Show("Property Age changed"); }
以上就是MVVMFoundation框架的主要使用方法,感興趣的人可以用用看~歡迎留言交流心得~