ObservableValue.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System;
  2. namespace IFramework
  3. {
  4. /// <summary>
  5. /// 可观测树值
  6. /// </summary>
  7. /// <typeparam name="T"></typeparam>
  8. public class ObservableValue<T> : ObservableObject
  9. {
  10. /// <summary>
  11. /// 默认的名字
  12. /// </summary>
  13. public const string ValuePropertyName = "value";
  14. private T _value;
  15. /// <summary>
  16. /// 具体的数值
  17. /// </summary>
  18. public T value
  19. {
  20. get { return GetProperty(ref _value, ValuePropertyName); }
  21. set
  22. {
  23. SetProperty(ref _value, value, ValuePropertyName);
  24. }
  25. }
  26. /// <summary>
  27. /// Ctor
  28. /// </summary>
  29. /// <param name="value"></param>
  30. public ObservableValue(T value) : base()
  31. {
  32. _value = value;
  33. }
  34. /// <summary>
  35. /// 注册 value 变化监听
  36. /// </summary>
  37. /// <param name="listener"></param>
  38. public void Subscribe(Action listener)
  39. {
  40. base.Subscribe(ValuePropertyName, listener);
  41. }
  42. /// <summary>
  43. /// 取消注册 value 变化监听
  44. /// </summary>
  45. /// <param name="listener"></param>
  46. public void UnSubscribe(Action listener)
  47. {
  48. base.UnSubscribe(ValuePropertyName, listener);
  49. }
  50. /// <summary>
  51. /// 方便书写,缩减代码
  52. /// </summary>
  53. /// <param name="value"></param>
  54. public static implicit operator T(ObservableValue<T> value)
  55. {
  56. return value.value;
  57. }
  58. }
  59. }