先科普一下:什么是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框架的主要使用方法,感兴趣的人能够用用看~欢迎留言交流心得~