using System; using Prism.Mvvm; using System.Linq; using Prism.Events; using Prism.Regions; using Prism.Commands; using Panuon.UI.Silver; using System.Windows.Controls; using SHJX.Service.Common.Event; using System.Collections.Generic; using SHJX.Service.Common.Constants; using SHJX.Service.Common.Interface; using SHJX.Service.Control.Interface; using System.Collections.ObjectModel; namespace SHJX.Service.Shell.ViewModels.Setting { public class SettingWindowViewModel : BindableBase { #region Field private IRegion _region; private readonly ILogService _log; private readonly IEventAggregator _ea; private readonly ISettingService _service; private readonly IRegionManager _regionManager; private Dictionary _items; #endregion #region Property public ObservableCollection TabItemInfos { get; set; } private bool? _dialogResult; public bool? DialogResult { get { return _dialogResult; } set => SetProperty(ref _dialogResult, value); } #endregion public SettingWindowViewModel(IEventAggregator ea, IRegionManager regionManager, ILogService log, ISettingService service) { _ea = ea; _log = log; _service = service; _regionManager = regionManager; TabItemInfos ??= new ObservableCollection(); InitDataAndBind(); } #region Command private DelegateCommand _loginLoadingCommand; public DelegateCommand LoginLoadingCommand => _loginLoadingCommand ??= new DelegateCommand(ExecuteLoginLoadingCommand); private DelegateCommand _menuSelectionChangedCommand; public DelegateCommand MenuSelectionChangedCommand => _menuSelectionChangedCommand ??= new DelegateCommand(ExecuteMenuSelectionChangedCommand); private DelegateCommand _closingCommand; public DelegateCommand ClosingCommand => _closingCommand ??= new DelegateCommand(() => { _ea.GetEvent().Publish(); _regionManager.Regions.Remove(RegionNames.SettingRegion);//关闭窗体之前先移除注册项 }); private DelegateCommand _updateSettingCommand; public DelegateCommand UpdateSettingCommand => _updateSettingCommand ??= new DelegateCommand(() => { _ea.GetEvent().Publish(); }); private DelegateCommand _cancelSettingUpdate; public DelegateCommand CancelSettingUpdate => _cancelSettingUpdate ??= new DelegateCommand(() => { DialogResult = true; }); #endregion #region Execute void ExecuteLoginLoadingCommand() { try { _region = _regionManager.Regions[RegionNames.SettingRegion]; _region.RequestNavigate(_items.FirstOrDefault().Value); } catch (Exception ex) { _log.ErrorFormat(ex.ToString()); } } public void ExecuteMenuSelectionChangedCommand(string elementName) { try { if (_items.TryGetValue(elementName, out var value)) { _region.RequestNavigate(value); } } catch (Exception ex) { _log.ErrorFormat(ex.ToString()); } } #endregion #region Method private void InitDataAndBind() { _items = _service.SettingTabItems; foreach (var item in _items.Keys) { TabItem tab = new() { Header = item }; TabItemInfos.Add(tab); } } #endregion } }