BaseEditorModel.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using Prism.Commands;
  2. using Prism.Mvvm;
  3. using Prism.Services.Dialogs;
  4. using SKMC.Api.Client.UI;
  5. using SKMC.Api.Common;
  6. using System;
  7. namespace SKMC.Api.Client.Views
  8. {
  9. /// <summary>
  10. /// 封装Editor基础类
  11. /// </summary>
  12. /// <typeparam name="T"></typeparam>
  13. public class BaseEditorModel<T> : BindableBase, IShowDialog
  14. {
  15. private readonly ClientCacher clientCacher = ObjectFactory.Resolve<ClientCacher>();
  16. public string Id { get; set; }
  17. public string Title { get; set; }
  18. public event Action<IDialogResult> RequestClose;
  19. public T Model { get; set; }
  20. public DelegateCommand SaveCommand { get; set; }
  21. public DelegateCommand CancelCommand { get; set; }
  22. public bool CanCloseDialog() => true;
  23. public BaseEditorModel()
  24. {
  25. SaveCommand = new DelegateCommand(() =>
  26. {
  27. DialogParameters parameters = new DialogParameters
  28. {
  29. { "Id", Id }
  30. };
  31. if (Model != null) parameters.Add("result", Model);
  32. RequestClose?.Invoke(new DialogResult(ButtonResult.OK, parameters));
  33. });
  34. CancelCommand = new DelegateCommand(() =>
  35. {
  36. DialogParameters parameters = new DialogParameters
  37. {
  38. { "Id", Id }
  39. };
  40. RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel, parameters));
  41. });
  42. }
  43. public void OnDialogClosed()
  44. {
  45. }
  46. public void OnDialogOpened(IDialogParameters parameters)
  47. {
  48. if (parameters.GetValue<T>("model") != null)
  49. {
  50. Model = parameters.GetValue<T>("model");
  51. }
  52. Id = CommonUtil.StringId();
  53. clientCacher.ShowCommonDialogs.Add(this);
  54. }
  55. public void CloseDialog()
  56. {
  57. DialogParameters parameters = new DialogParameters
  58. {
  59. { "Id", Id }
  60. };
  61. if (Model != null) parameters.Add("result", Model);
  62. RequestClose?.Invoke(new DialogResult(ButtonResult.OK, parameters));
  63. }
  64. }
  65. }