BindableObject.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Runtime.CompilerServices;
  5. namespace IFramework
  6. {
  7. /// <summary>
  8. /// 绑定对象
  9. /// </summary>
  10. public abstract class BindableObject : Unit
  11. {
  12. /// <summary>
  13. /// 绑定方式
  14. /// </summary>
  15. public enum BindOperation
  16. {
  17. /// <summary>
  18. /// 监听+发布
  19. /// </summary>
  20. Both,
  21. /// <summary>
  22. /// 监听
  23. /// </summary>
  24. Listen,
  25. }
  26. /// <summary>
  27. /// 绑定方式
  28. /// </summary>
  29. public BindOperation bindOperation = BindOperation.Both;
  30. private Dictionary<string, Action<string, object>> _callmap;
  31. /// <summary>
  32. /// ctor
  33. /// </summary>
  34. protected BindableObject()
  35. {
  36. _callmap = new Dictionary<string, Action<string, object>>();
  37. }
  38. /// <summary>
  39. /// 注册监听
  40. /// </summary>
  41. /// <param name="propertyName"></param>
  42. /// <param name="listener"></param>
  43. public void Subscribe(string propertyName, Action<string, object> listener)
  44. {
  45. if (!_callmap.ContainsKey(propertyName))
  46. _callmap.Add(propertyName, null);
  47. _callmap[propertyName] += listener;
  48. }
  49. /// <summary>
  50. /// 移除监听
  51. /// </summary>
  52. /// <param name="propertyName"></param>
  53. /// <param name="listener"></param>
  54. public void UnSubscribe(string propertyName, Action<string, object> listener)
  55. {
  56. if (!_callmap.ContainsKey(propertyName))
  57. return;
  58. _callmap[propertyName] -= listener;
  59. if (_callmap[propertyName] == null)
  60. _callmap.Remove(propertyName);
  61. }
  62. /// <summary>
  63. /// 获取属性
  64. /// </summary>
  65. /// <typeparam name="T"></typeparam>
  66. /// <param name="property"></param>
  67. /// <param name="propertyName"></param>
  68. /// <returns></returns>
  69. protected T GetProperty<T>(ref T property, [CallerMemberName]string propertyName = "")
  70. {
  71. if (BindableObjectHandler.handler != null)
  72. {
  73. //if (string.IsNullOrEmpty(propertyName))
  74. // propertyName = GetProperyName(new StackTrace(true).GetFrame(1).GetMethod().Name);
  75. BindableObjectHandler.handler.Subscribe(this, typeof(T), propertyName);
  76. }
  77. return property;
  78. }
  79. /// <summary>
  80. /// 设置属性
  81. /// </summary>
  82. /// <typeparam name="T"></typeparam>
  83. /// <param name="property"></param>
  84. /// <param name="value"></param>
  85. /// <param name="propertyName"></param>
  86. protected void SetProperty<T>(ref T property, T value, [CallerMemberName]string propertyName = "")
  87. {
  88. if (EqualityComparer<T>.Default.Equals(property, value)) return;
  89. //if (string.IsNullOrEmpty(propertyName))
  90. // propertyName = GetProperyName(new StackTrace(true).GetFrame(1).GetMethod().Name);
  91. property = value;
  92. //if (bindOperation == BindOperation.Listen) return;
  93. PublishPropertyChange(propertyName, value);
  94. }
  95. private void PublishPropertyChange(string propertyName, object obj)
  96. {
  97. if (!_callmap.ContainsKey(propertyName)) return;
  98. if (_callmap[propertyName] == null) return;
  99. _callmap[propertyName].Invoke(propertyName, obj);
  100. }
  101. /// <summary>
  102. /// 释放
  103. /// </summary>
  104. protected override void OnDispose()
  105. {
  106. _callmap.Clear();
  107. _callmap = null;
  108. }
  109. //private string GetProperyName(string methodName)
  110. //{
  111. // if (methodName.StartsWith("get_") || methodName.StartsWith("set_") ||
  112. // methodName.StartsWith("put_"))
  113. // {
  114. // return methodName.Substring("get_".Length);
  115. // }
  116. // throw new Exception(methodName + " not a method of Property");
  117. //}
  118. }
  119. }