ObservableObject.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Runtime.CompilerServices;
  5. namespace IFramework
  6. {
  7. /// <summary>
  8. /// 可观测 Object
  9. /// </summary>
  10. public abstract class ObservableObject : Unit
  11. {
  12. private Dictionary<string, Action> _callmap;
  13. /// <summary>
  14. /// Ctor
  15. /// </summary>
  16. protected ObservableObject()
  17. {
  18. _callmap = new Dictionary<string, Action>();
  19. }
  20. /// <summary>
  21. /// 注册数值变化监听
  22. /// </summary>
  23. /// <param name="propertyName"></param>
  24. /// <param name="listener"></param>
  25. public void Subscribe(string propertyName, Action listener)
  26. {
  27. if (!_callmap.ContainsKey(propertyName))
  28. _callmap.Add(propertyName, null);
  29. _callmap[propertyName] += listener;
  30. }
  31. /// <summary>
  32. /// 取消注册数值变化监听
  33. /// </summary>
  34. /// <param name="propertyName"></param>
  35. /// <param name="listener"></param>
  36. public void UnSubscribe(string propertyName, Action listener)
  37. {
  38. if (!_callmap.ContainsKey(propertyName))
  39. throw new Exception($"Have not Subscribe {propertyName}");
  40. _callmap[propertyName] -= listener;
  41. if (_callmap[propertyName] == null)
  42. _callmap.Remove(propertyName);
  43. }
  44. /// <summary>
  45. /// 获取属性
  46. /// </summary>
  47. /// <typeparam name="T"></typeparam>
  48. /// <param name="property">获取的属性</param>
  49. /// <param name="propertyName">属性名称</param>
  50. /// <returns></returns>
  51. protected T GetProperty<T>(ref T property, [CallerMemberName]string propertyName = "")
  52. {
  53. if (ObservableObjectHandler.handler != null)
  54. {
  55. //if (string.IsNullOrEmpty(propertyName))
  56. // propertyName = GetProperyName(new StackTrace(true).GetFrame(1).GetMethod().Name);
  57. ObservableObjectHandler.handler.Subscribe(this, propertyName);
  58. }
  59. return property;
  60. }
  61. /// <summary>
  62. /// 设置属性
  63. /// </summary>
  64. /// <typeparam name="T"></typeparam>
  65. /// <param name="property">赋值的变量</param>
  66. /// <param name="value">变化的值</param>
  67. /// <param name="propertyName">属性名称</param>
  68. protected void SetProperty<T>(ref T property, T value, [CallerMemberName]string propertyName = "")
  69. {
  70. if (EqualityComparer<T>.Default.Equals(property, value)) return;
  71. //if (string.IsNullOrEmpty(propertyName))
  72. // propertyName = GetProperyName(new StackTrace(true).GetFrame(1).GetMethod().Name);
  73. property = value;
  74. PublishPropertyChange(propertyName);
  75. }
  76. /// <summary>
  77. /// 发布属性发生变化
  78. /// </summary>
  79. /// <param name="propertyName">属性名称</param>
  80. protected void PublishPropertyChange(string propertyName)
  81. {
  82. if (!_callmap.ContainsKey(propertyName)) return;
  83. if (_callmap[propertyName] == null) return;
  84. _callmap[propertyName].Invoke();
  85. }
  86. /// <summary>
  87. /// 释放时
  88. /// </summary>
  89. protected override void OnDispose()
  90. {
  91. _callmap.Clear();
  92. _callmap = null;
  93. }
  94. //private string GetProperyName(string methodName)
  95. //{
  96. // if (methodName.StartsWith("get_") || methodName.StartsWith("set_") ||
  97. // methodName.StartsWith("put_"))
  98. // {
  99. // return methodName.Substring("get_".Length);
  100. // }
  101. // throw new Exception(methodName + " not a method of Property");
  102. //}
  103. }
  104. }