Welcome

首页 / 软件开发 / C# / Windows 8开发入门(二十一) Windows 8 下进行MVVM开发

Windows 8开发入门(二十一) Windows 8 下进行MVVM开发2013-12-02 cnblogs 程兴亮在本文中将演示如何在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);}}