先科普一下:什麼是WPF,請看下圖設計模式
微軟對於WPF技術的構想是很宏大的,惋惜普及率不高,不過若是你要作Windows客戶端開發的話WPF技術仍是值得一學的。app
簡單來講它是一種高級的UI設計模式。據我所知目前還運用在一些js框架中,好比AngularJS。其餘的UI設計模式還包括MVC、MVP,我的以爲最強大的仍是MVVM。框架
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 .
簡單翻譯一下(不全)ide
- The Model 表明業務邏輯的實體類,能夠是一個簡單的顧客實體類,也能夠是一個複雜的股票交易實體類。
- The View 表明一個用戶界面控件 ...
- The ViewModel 包括各類邏輯、命令、事件以及實體類的引用。
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 developersui
MVVMFoundation包含四大模塊:this
ObservableObject:這裏至關於ViewModelBase的概念,每個ViewModel繼承自該類,調用完成以後當即釋放,防止內存泄露。翻譯
RelayCommand接口:封裝command的聲明,包括execution執行邏輯,可選的can-execute邏輯等。外部只須要實例化並Binding就能夠簡單使用。設計
Messenger:這裏主要用在各類不一樣的ViewModel之間通訊(好比相互關聯的ViewModel、主從ViewModel等),固然也能夠擴展成ViewModel與View之間進行通訊。
PropertyObserver:主要是對INotifyPropertyChanged.PropertyChanged進行封裝,能夠經過其對某個對象的屬性變動註冊回調函數,當屬性變動時便觸發回調函數。
實現ViewModel中的屬性改變通知到綁定的控件的方法,至關因而全部Viewmodel的基類。
使用時調用OnPropertyChange方法,則後臺數據變化便可通知界面刷新
public string UserName { get { return this.user.UserName; } set { this.user.UserName = value; OnPropertyChanged("UserName"); } }
屬性在View界面的綁定
<TextBox Text="{Binding UserName}" />
用於在ViewModel中定義View中綁定的命令,代替了之前Winform的Click事件。
在ViewModel中定義Command
public ICommand BrowseImageCommand { get { return new ICommand(BrowseImage); } } private void BrowseImage() { ... }
在View中的按鈕關聯此Command
<Button Content="瀏覽..." Command="{Binding BrowseImageCommand}"/>
可用於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<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框架的主要使用方法,感興趣的人能夠用用看~歡迎留言交流心得~