123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
-
- 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;
- /// <summary>
- /// 返回所有的命令
- /// </summary>
- 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<string, object> m_DicRef=new Dictionary<string, object>();
- /// <summary>
- /// 注册父窗体
- /// </summary>
- public void Register(string key, object refObject)
- {
- m_DicRef[key] = refObject;
- }
- /// <summary>
- /// 获取关联对象
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="key"></param>
- /// <returns></returns>
- public T GetRef<T>(string key)
- {
- if (!m_DicRef.ContainsKey(key))
- return default(T);
- return (T)m_DicRef[key];
- }
- private const string VIEWKEY = "{8526A703-2562-4613-896D-C97E362AA238}";
- /// <summary>
- /// 设置相关联view
- /// </summary>
- /// <param name="view"></param>
- public void SetRefView(UIElement view)
- {
- Register(VIEWKEY, view);
- }
- public UIElement GetRefView()
- {
- return GetRef<UIElement>(VIEWKEY);
- }
- #endregion
- #region 关联前台是否可用
- private Func<bool> m_WatchValidate;
- public void WatchValidateResult(Func<bool> action)
- {
- m_WatchValidate = action;
- }
- /// <summary>
- /// 界面验证是否通过
- /// </summary>
- 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
- }
- }
|