SettingWindowViewModel.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using Prism.Mvvm;
  3. using System.Linq;
  4. using Prism.Events;
  5. using Prism.Regions;
  6. using Prism.Commands;
  7. using Panuon.UI.Silver;
  8. using System.Windows.Controls;
  9. using SHJX.Service.Common.Event;
  10. using System.Collections.Generic;
  11. using SHJX.Service.Common.Constants;
  12. using SHJX.Service.Common.Interface;
  13. using SHJX.Service.Control.Interface;
  14. using System.Collections.ObjectModel;
  15. namespace SHJX.Service.Shell.ViewModels.Setting
  16. {
  17. public class SettingWindowViewModel : BindableBase
  18. {
  19. #region Field
  20. private IRegion _region;
  21. private readonly ILogService _log;
  22. private readonly IEventAggregator _ea;
  23. private readonly ISettingService _service;
  24. private readonly IRegionManager _regionManager;
  25. private Dictionary<string, string> _items;
  26. #endregion
  27. #region Property
  28. public ObservableCollection<TabItem> TabItemInfos { get; set; }
  29. private bool? _dialogResult;
  30. public bool? DialogResult
  31. {
  32. get { return _dialogResult; }
  33. set => SetProperty(ref _dialogResult, value);
  34. }
  35. #endregion
  36. public SettingWindowViewModel(IEventAggregator ea, IRegionManager regionManager, ILogService log, ISettingService service)
  37. {
  38. _ea = ea;
  39. _log = log;
  40. _service = service;
  41. _regionManager = regionManager;
  42. TabItemInfos ??= new ObservableCollection<TabItem>();
  43. InitDataAndBind();
  44. }
  45. #region Command
  46. private DelegateCommand _loginLoadingCommand;
  47. public DelegateCommand LoginLoadingCommand => _loginLoadingCommand ??= new DelegateCommand(ExecuteLoginLoadingCommand);
  48. private DelegateCommand<string> _menuSelectionChangedCommand;
  49. public DelegateCommand<string> MenuSelectionChangedCommand => _menuSelectionChangedCommand ??= new DelegateCommand<string>(ExecuteMenuSelectionChangedCommand);
  50. private DelegateCommand _closingCommand;
  51. public DelegateCommand ClosingCommand => _closingCommand ??= new DelegateCommand(() =>
  52. {
  53. _ea.GetEvent<SettingClosingEvent>().Publish();
  54. _regionManager.Regions.Remove(RegionNames.SettingRegion);//关闭窗体之前先移除注册项
  55. });
  56. private DelegateCommand _updateSettingCommand;
  57. public DelegateCommand UpdateSettingCommand => _updateSettingCommand ??= new DelegateCommand(() =>
  58. {
  59. _ea.GetEvent<UpdateSettingEvent>().Publish();
  60. });
  61. private DelegateCommand _cancelSettingUpdate;
  62. public DelegateCommand CancelSettingUpdate => _cancelSettingUpdate ??= new DelegateCommand(() =>
  63. {
  64. DialogResult = true;
  65. });
  66. #endregion
  67. #region Execute
  68. void ExecuteLoginLoadingCommand()
  69. {
  70. try
  71. {
  72. _region = _regionManager.Regions[RegionNames.SettingRegion];
  73. _region.RequestNavigate(_items.FirstOrDefault().Value);
  74. }
  75. catch (Exception ex)
  76. {
  77. _log.ErrorFormat(ex.ToString());
  78. }
  79. }
  80. public void ExecuteMenuSelectionChangedCommand(string elementName)
  81. {
  82. try
  83. {
  84. if (_items.TryGetValue(elementName, out var value))
  85. {
  86. _region.RequestNavigate(value);
  87. }
  88. }
  89. catch (Exception ex)
  90. {
  91. _log.ErrorFormat(ex.ToString());
  92. }
  93. }
  94. #endregion
  95. #region Method
  96. private void InitDataAndBind()
  97. {
  98. _items = _service.SettingTabItems;
  99. foreach (var item in _items.Keys)
  100. {
  101. TabItem tab = new()
  102. {
  103. Header = item
  104. };
  105. TabItemInfos.Add(tab);
  106. }
  107. }
  108. #endregion
  109. }
  110. }