WPF MVVM设计模式的ViewModelBase和CommandBase代码
ViewModelBase和CommandBase代码的整理如下:
1.ViewModelBase.cs
- using System;
- using System.ComponentModel;
- using System.Linq.Expressions;
- /// <summary>
- /// WPF MVVC设计模式ViewMode基本功能类
- /// </summary>
- public abstract class ViewModelBase : INotifyPropertyChanged, IDisposable
- {
- #region Public Properties
- /// <summary>
- /// 显示名称
- /// </summary>
- public virtual string DisplayName { get; protected set; }
- #endregion
- #region Constructor
- /// <summary>
- /// 实例化一个ViewModelBase对象
- /// </summary>
- protected ViewModelBase()
- {
- }
- #endregion
- #region INotifyPropertyChanged Members
- /// <summary>
- /// 触发属性发生变更事件
- /// </summary>
- /// <typeparam name="T">泛型标记,会匹配函数返回类型,不必手动填写</typeparam>
- /// <param name="action">以函数表达式方式传入属性名称,表达式如下即可:()=>YourViewModelProperty</param>
- protected void OnPropertyChanged<T>(Expression<Func<T>> action)
- {
- var propertyName = GetPropertyName(action);
- OnPropertyChanged(propertyName);
- }
- private static string GetPropertyName<T>(Expression<Func<T>> action)
- {
- var expression = (MemberExpression)action.Body;
- var propertyName = expression.Member.Name;
- return propertyName;
- }
- private void OnPropertyChanged(string propertyName)
- {
- PropertyChangedEventHandler handler = PropertyChanged;
- if (handler != null)
- {
- var e = new PropertyChangedEventArgs(propertyName);
- handler(this, e);
- }
- }
- public event PropertyChangedEventHandler PropertyChanged;
- #endregion
- #region IDisposable Members
- public void Dispose()
- {
- this.OnDispose();
- }
- /// <summary>
- /// 若支持IDisposable,请重写此方法,当被调用Dispose时会执行此方法。
- /// </summary>
- protected virtual void OnDispose()
- {
- }
- #endregion
- }
2.CommandBase.cs
- using System;
- using System.Windows.Input;
- /// <summary>
- /// WPF MVVC设计模式命令基本功能类
- /// </summary>
- public class CommandBase : ICommand
- {
- #region Private Fields
- private readonly Action<object> _command;
- private readonly Func<object, bool> _canExecute;
- #endregion
- #region Constructor
- /// <summary>
- /// 实例化一个CommandBase对象
- /// </summary>
- /// <param name="command">委托一个有object类型参数的命令执行函数</param>
- /// <param name="canExecute">委托一个有object类型参数的命令是否能被执行的函数(可选)</param>
- /// <exception cref="ArgumentNullException">参数command不可以为null引用</exception>
- public CommandBase(Action<object> command, Func<object, bool> canExecute = null)
- {
- if (command == null)
- throw new ArgumentNullException("command");
- _canExecute = canExecute;
- _command = command;
- }
- #endregion
- #region ICommand Members
- public void Execute(object parameter)
- {
- _command(parameter);
- }
- public bool CanExecute(object parameter)
- {
- if (_canExecute == null)
- return true;
- return _canExecute(parameter);
- }
- public event EventHandler CanExecuteChanged
- {
- add { CommandManager.RequerySuggested += value; }
- remove { CommandManager.RequerySuggested -= value; }
- }
- #endregion
- }
关于WPF MVVM设计模式文档(Josh Smith),请阅读:http://msdn.microsoft.com/zh-cn/magazine/dd419663.aspx
此代码是Josh Smith的范例修改版本。