在本文中将演示如何在Windows 8进行MVVM开发,首先我们准备两个辅助类如下:
ViewModeBase类 :
public class ViewModeBase : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; /// <summary> /// 属性变化时触发事件 /// </summary> /// <param name="propertyName"></param> protected void OnPropertyChanged( string propertyName ) { var handler = this.PropertyChanged; if ( handler != null ) { handler( this, new PropertyChangedEventArgs( propertyName ) ); } } }
DelegateCommand类:
public class DelegateCommand : ICommand { private readonly Action m_exeAction; private readonly Func<bool> m_canExeAction; /// <summary> /// 构造函数 /// </summary> /// <param name="executeAction"></param> /// <param name="canExecuteAction"></param> public DelegateCommand(Action executeAction, Func<bool> canExecuteAction) { m_exeAction = executeAction; m_canExeAction = canExecuteAction; } /// <summary> /// 构造函数 /// </summary> /// <param name="executeAction"></param> /// <param name="canExecuteAction"></param> public DelegateCommand(Action executeAction) : this(executeAction, null) { } /// <summary> /// 判断是否执行操作 /// </summary> /// <param name="parameter"></param> /// <returns></returns> public bool CanExecute(object parameter) { if (m_canExeAction != null) { return m_canExeAction(); } return true; } /// <summary> /// 是否执行操作的变更发生时 /// </summary> public event EventHandler CanExecuteChanged; /// <summary> /// 执行操作的内容,可以变为Action行为 /// </summary> /// <param name="parameter"></param> public void Execute(object parameter) { if (CanExecute(parameter)) { m_exeAction(); } } protected void OnCanExecuteChanged(object sender, EventArgs args) { var handler = CanExecuteChanged; if ( handler != null ) { handler( this, EventArgs.Empty ); } } public void RaiseCanExecuteChanged() { OnCanExecuteChanged(this, EventArgs.Empty); } }
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索mvvm开发
, action
, mvvm
, handler
, this
, public
parameter
windows开发入门、windows驱动开发入门、wpf mvvm入门、mvvm入门与提高、mvvm入门,以便于您获取更多的相关知识。
时间: 2024-12-02 10:49:44