BaseEditorModel.cs 2.2 KB

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