using System; using System.Collections.Generic; using System.Windows.Controls; #if netle40 using GalaSoft.MvvmLight.Command; #else using GalaSoft.MvvmLight.CommandWpf; #endif using GalaSoft.MvvmLight.Messaging; using HandyControl.Controls; using HandyControlDemo.Data; using HandyControlDemo.Service; using HandyControlDemo.Tools; using HandyControlDemo.UserControl; namespace HandyControlDemo.ViewModel { public class MainViewModel : DemoViewModelBase { #region 字段 /// /// 内容标题 /// private object _contentTitle; /// /// 子内容 /// private object _subContent; /// /// demo信息 /// private List _demoInfoList; #endregion public MainViewModel(DataService dataService) { Messenger.Default.Register(this, MessageToken.LoadShowContent, obj => { if (SubContent is IDisposable disposable) { disposable.Dispose(); } SubContent = obj; }); Messenger.Default.Register(this, MessageToken.ClearLeftSelected, obj => { DemoItemCurrent = null; foreach (var item in DemoInfoList) { item.SelectedIndex = -1; } }); DataList = dataService.GetDemoDataList(); DemoInfoList = dataService.GetDemoInfo(); } #region 属性 /// /// 当前选中的demo项 /// public DemoItemModel DemoItemCurrent { get; private set; } public DemoInfoModel DemoInfoCurrent { get; set; } /// /// 子内容 /// public object SubContent { get => _subContent; #if netle40 set => Set(nameof(SubContent), ref _subContent, value); #else set => Set(ref _subContent, value); #endif } /// /// 内容标题 /// public object ContentTitle { get => _contentTitle; #if netle40 set => Set(nameof(ContentTitle), ref _contentTitle, value); #else set => Set(ref _contentTitle, value); #endif } /// /// demo信息 /// public List DemoInfoList { get => _demoInfoList; #if netle40 set => Set(nameof(DemoInfoList), ref _demoInfoList, value); #else set => Set(ref _demoInfoList, value); #endif } #endregion #region 命令 /// /// 切换例子命令 /// public RelayCommand SwitchDemoCmd => new Lazy>(() => new RelayCommand(SwitchDemo)).Value; /// /// 打开概览命令 /// public RelayCommand OpenOverviewCmd => new Lazy(() => new RelayCommand(OpenOverview)).Value; public RelayCommand GlobalShortcutInfoCmd => new Lazy(() => new RelayCommand(() => Growl.Info("Global Shortcut Info"))).Value; public RelayCommand GlobalShortcutWarningCmd => new Lazy(() => new RelayCommand(() => Growl.Warning("Global Shortcut Warning"))).Value; #endregion #region 方法 /// /// 切换例子 /// private void SwitchDemo(SelectionChangedEventArgs e) { if (e.AddedItems.Count == 0) return; if (e.AddedItems[0] is DemoItemModel item) { if (Equals(DemoItemCurrent, item)) return; DemoItemCurrent = item; ContentTitle = item.Name; var obj = AssemblyHelper.ResolveByKey(item.TargetCtlName); var ctl = obj ?? AssemblyHelper.CreateInternalInstance($"UserControl.{item.TargetCtlName}"); Messenger.Default.Send(ctl is IFull, MessageToken.FullSwitch); Messenger.Default.Send(ctl, MessageToken.LoadShowContent); } } /// /// 打开概览 /// private void OpenOverview() { Messenger.Default.Send(null, MessageToken.ClearLeftSelected); Messenger.Default.Send(true, MessageToken.FullSwitch); Messenger.Default.Send(AssemblyHelper.CreateInternalInstance($"UserControl.{MessageToken.OverView}"), MessageToken.LoadShowContent); } #endregion } }