ObservableObjectHandler.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.Collections.Generic;
  3. namespace IFramework
  4. {
  5. /// <summary>
  6. /// ObservableObject 注册监听Helper
  7. /// </summary>
  8. public class ObservableObjectHandler : Unit
  9. {
  10. struct ObserveEntity
  11. {
  12. private string _propertyName;
  13. private ObservableObject _observableObject;
  14. private Action _listenner;
  15. public string propertyName { get { return _propertyName; } }
  16. public ObservableObject observableObject { get { return _observableObject; } }
  17. public ObserveEntity(ObservableObject obj, string propertyName, Action listenner)
  18. {
  19. this._propertyName = propertyName;
  20. this._observableObject = obj;
  21. this._listenner = listenner;
  22. }
  23. public void Bind()
  24. {
  25. observableObject.Subscribe(propertyName, _listenner);
  26. }
  27. public void UnBind()
  28. {
  29. observableObject.UnSubscribe(propertyName, _listenner);
  30. }
  31. }
  32. internal static ObservableObjectHandler handler { get; private set; }
  33. private List<ObserveEntity> _entitys = new List<ObserveEntity>();
  34. private Action listenner;
  35. internal ObservableObjectHandler Subscribe(ObservableObject _object, string propertyName)
  36. {
  37. Subscribe(_object, propertyName, listenner);
  38. return this;
  39. }
  40. /// <summary>
  41. /// 对一个 ObservableObject 注册一个监听
  42. /// </summary>
  43. /// <param name="_object"> ObservableObject </param>
  44. /// <param name="propertyName">属性名称</param>
  45. /// <param name="listenner">回调</param>
  46. /// <returns></returns>
  47. public ObservableObjectHandler Subscribe(ObservableObject _object, string propertyName, Action listenner)
  48. {
  49. var bindTarget = new ObserveEntity(_object, propertyName, listenner);
  50. bindTarget.Bind();
  51. _entitys.Add(bindTarget);
  52. return this;
  53. }
  54. /// <summary>
  55. /// 绑定一个监听
  56. /// </summary>
  57. /// <param name="setter"> 回调</param>
  58. /// <returns></returns>
  59. public ObservableObjectHandler BindProperty(Action setter)
  60. {
  61. this.listenner = setter;
  62. handler = this;
  63. setter.Invoke();
  64. listenner = null;
  65. handler = null;
  66. return this;
  67. }
  68. /// <summary>
  69. /// 绑定一个监听
  70. /// </summary>
  71. /// <typeparam name="T"></typeparam>
  72. /// <param name="setter"> 设置值 </param>
  73. /// <param name="getter"> 获取值 </param>
  74. /// <returns></returns>
  75. public ObservableObjectHandler BindProperty<T>(Action<T> setter, Func<T> getter)
  76. {
  77. this.listenner = () => { setter(getter()); };
  78. setter(AddExpressionListener(getter));
  79. return this;
  80. }
  81. private T AddExpressionListener<T>(Func<T> expression)
  82. {
  83. handler = this;
  84. var result = expression.Invoke();
  85. handler = null;
  86. listenner = null;
  87. return result;
  88. }
  89. /// <summary>
  90. /// 取消所有监听
  91. /// </summary>
  92. public void UnSubscribe()
  93. {
  94. _entitys.ForEach((entity) =>
  95. {
  96. entity.UnBind();
  97. });
  98. _entitys.Clear();
  99. }
  100. /// <summary>
  101. /// 取消符合条件的监听
  102. /// </summary>
  103. /// <param name="_object"> ObservableObject </param>
  104. /// <param name="propertyName"> 属性名称 </param>
  105. public void UnSubscribe(ObservableObject _object, string propertyName)
  106. {
  107. var result = _entitys.RemoveAll((entity) =>
  108. {
  109. if (entity.observableObject != _object || entity.propertyName != propertyName) return false;
  110. entity.UnBind();
  111. return true;
  112. });
  113. }
  114. /// <summary>
  115. /// 释放
  116. /// </summary>
  117. protected override void OnDispose()
  118. {
  119. UnSubscribe();
  120. }
  121. }
  122. }