View.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using IFramework.Message;
  2. using System;
  3. namespace IFramework.MVVM
  4. {
  5. /// <summary>
  6. /// 界面
  7. /// </summary>
  8. public abstract class View : IDisposable
  9. {
  10. private ObservableValue<ViewModel> _context = new ObservableValue<ViewModel>(null);
  11. /// <summary>
  12. /// 数据绑定
  13. /// </summary>
  14. protected ObservableObjectHandler handler;
  15. /// <summary>
  16. /// VM
  17. /// </summary>
  18. public ViewModel context
  19. {
  20. get { return _context; }
  21. set
  22. {
  23. _context.value = value;
  24. }
  25. }
  26. /// <summary>
  27. /// ctor
  28. /// </summary>
  29. public View()
  30. {
  31. handler = new ObservableObjectHandler();
  32. _context.Subscribe(BindProperty);
  33. }
  34. /// <summary>
  35. /// 绑定数据
  36. /// </summary>
  37. protected virtual void BindProperty()
  38. {
  39. handler.UnSubscribe();
  40. }
  41. /// <summary>
  42. /// 释放
  43. /// </summary>
  44. public void Dispose()
  45. {
  46. OnDispose();
  47. handler.UnSubscribe();
  48. }
  49. /// <summary>
  50. /// 释放时
  51. /// </summary>
  52. protected virtual void OnDispose() { }
  53. /// <summary>
  54. /// 发布消息
  55. /// </summary>
  56. /// <param name="message"></param>
  57. protected void Publish(IEventArgs message)
  58. {
  59. (context as IViewModel).Listen(message);
  60. }
  61. }
  62. /// <summary>
  63. /// 方便书写
  64. /// </summary>
  65. /// <typeparam name="T"></typeparam>
  66. public abstract class View<T> : View where T : ViewModel
  67. {
  68. /// <summary>
  69. /// 方便书写
  70. /// </summary>
  71. public T Tcontext { get { return context as T; } set { context = value; } }
  72. }
  73. }