using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Windows; using FWindSoft.Data; namespace FWindSoft.MVVM { public class BaseViewModel : BasePropertyChanged { /*此所谓适配器,为了提供统一的访问接口 * 其充当的角色不过是一个抽象引用。 * 依托于wpf的反射机制,动态调用vm中方法的名称 * 其本质无所谓适配器 */ private CommandAdapter m_Commands; /// /// 返回所有的命令 /// public CommandAdapter Commands { get { if (m_Commands == null) { Type t = typeof(CommandAdapterFactory<>).MakeGenericType(new Type[] { this.GetType() }); m_Commands = (CommandAdapter)(t.GetMember("Create").FirstOrDefault() as MethodInfo) .Invoke(null, new object[] { this }); } return m_Commands; } } #region 注册相关 protected Dictionary m_DicRef=new Dictionary(); /// /// 注册父窗体 /// public void Register(string key, object refObject) { m_DicRef[key] = refObject; } /// /// 获取关联对象 /// /// /// /// public T GetRef(string key) { if (!m_DicRef.ContainsKey(key)) return default(T); return (T)m_DicRef[key]; } private const string VIEWKEY = "{8526A703-2562-4613-896D-C97E362AA238}"; /// /// 设置相关联view /// /// public void SetRefView(UIElement view) { Register(VIEWKEY, view); } public UIElement GetRefView() { return GetRef(VIEWKEY); } #endregion #region 关联前台是否可用 private Func m_WatchValidate; public void WatchValidateResult(Func action) { m_WatchValidate = action; } /// /// 界面验证是否通过 /// public override bool IsValidated { get { if (!base.IsValidated) return false;//验证通过可以走下面的逻辑 var temp = m_WatchValidate; if (temp != null) { return temp(); } return true; } } #endregion #region 内部属性变更模块 public bool IsInEditing { get;private set; } public void BeginInEdit() { IsInEditing = true; } public void EndInEdit() { IsInEditing = false; } #endregion } }