BaseViewModel.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. 
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Windows;
  7. using FWindSoft.Data;
  8. namespace FWindSoft.MVVM
  9. {
  10. public class BaseViewModel : BasePropertyChanged
  11. {
  12. /*此所谓适配器,为了提供统一的访问接口
  13. * 其充当的角色不过是一个抽象引用。
  14. * 依托于wpf的反射机制,动态调用vm中方法的名称
  15. * 其本质无所谓适配器
  16. */
  17. private CommandAdapter m_Commands;
  18. /// <summary>
  19. /// 返回所有的命令
  20. /// </summary>
  21. public CommandAdapter Commands
  22. {
  23. get
  24. {
  25. if (m_Commands == null)
  26. {
  27. Type t = typeof(CommandAdapterFactory<>).MakeGenericType(new Type[] { this.GetType() });
  28. m_Commands = (CommandAdapter)(t.GetMember("Create").FirstOrDefault() as MethodInfo)
  29. .Invoke(null, new object[] { this });
  30. }
  31. return m_Commands;
  32. }
  33. }
  34. #region 注册相关
  35. protected Dictionary<string, object> m_DicRef=new Dictionary<string, object>();
  36. /// <summary>
  37. /// 注册父窗体
  38. /// </summary>
  39. public void Register(string key, object refObject)
  40. {
  41. m_DicRef[key] = refObject;
  42. }
  43. /// <summary>
  44. /// 获取关联对象
  45. /// </summary>
  46. /// <typeparam name="T"></typeparam>
  47. /// <param name="key"></param>
  48. /// <returns></returns>
  49. public T GetRef<T>(string key)
  50. {
  51. if (!m_DicRef.ContainsKey(key))
  52. return default(T);
  53. return (T)m_DicRef[key];
  54. }
  55. private const string VIEWKEY = "{8526A703-2562-4613-896D-C97E362AA238}";
  56. /// <summary>
  57. /// 设置相关联view
  58. /// </summary>
  59. /// <param name="view"></param>
  60. public void SetRefView(UIElement view)
  61. {
  62. Register(VIEWKEY, view);
  63. }
  64. public UIElement GetRefView()
  65. {
  66. return GetRef<UIElement>(VIEWKEY);
  67. }
  68. #endregion
  69. #region 关联前台是否可用
  70. private Func<bool> m_WatchValidate;
  71. public void WatchValidateResult(Func<bool> action)
  72. {
  73. m_WatchValidate = action;
  74. }
  75. /// <summary>
  76. /// 界面验证是否通过
  77. /// </summary>
  78. public override bool IsValidated
  79. {
  80. get
  81. {
  82. if (!base.IsValidated)
  83. return false;//验证通过可以走下面的逻辑
  84. var temp = m_WatchValidate;
  85. if (temp != null)
  86. {
  87. return temp();
  88. }
  89. return true;
  90. }
  91. }
  92. #endregion
  93. #region 内部属性变更模块
  94. public bool IsInEditing { get;private set; }
  95. public void BeginInEdit()
  96. { IsInEditing = true; }
  97. public void EndInEdit()
  98. { IsInEditing = false; }
  99. #endregion
  100. }
  101. }