ViewModel.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using IFramework.Message;
  2. namespace IFramework.MVVM
  3. {
  4. internal interface IViewModel
  5. {
  6. void Initialize();
  7. void SyncModelValue();
  8. void Listen(IEventArgs message);
  9. }
  10. /// <summary>
  11. /// VM
  12. /// </summary>
  13. public abstract class ViewModel : ObservableObject, IViewModel
  14. {
  15. internal MVVMGroup group { get; set; }
  16. /// <summary>
  17. /// 数据
  18. /// </summary>
  19. protected IModel model { get { return group.model; } }
  20. void IViewModel.SyncModelValue()
  21. {
  22. SyncModelValue();
  23. }
  24. void IViewModel.Initialize()
  25. {
  26. Initialize();
  27. }
  28. void IViewModel.Listen(IEventArgs message)
  29. {
  30. Listen(message);
  31. }
  32. /// <summary>
  33. /// 初始化
  34. /// </summary>
  35. protected abstract void Initialize();
  36. /// <summary>
  37. /// 同步model数据
  38. /// </summary>
  39. protected abstract void SyncModelValue();
  40. /// <summary>
  41. /// 来自于view的消息
  42. /// </summary>
  43. /// <param name="message"></param>
  44. protected abstract void Listen(IEventArgs message);
  45. /// <summary>
  46. /// 释放时
  47. /// </summary>
  48. protected override void OnDispose() { }
  49. }
  50. /// <summary>
  51. /// 方便书写
  52. /// </summary>
  53. public abstract class ViewModel<T> : ViewModel where T : IModel
  54. {
  55. /// <summary>
  56. /// 方便书写
  57. /// </summary>
  58. protected T Tmodel { get { return (T)group.model; } }
  59. }
  60. }