using System; using System.Collections.ObjectModel; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Threading; using GalaSoft.MvvmLight.Command; using GalaSoft.MvvmLight.Messaging; using HandyControl.Controls; using HandyControlDemo.Data; using HandyControlDemo.Properties.Langs; using HandyControlDemo.Service; using HandyControlDemo.Tools; using HandyControlDemo.UserControl; namespace HandyControlDemo.ViewModel { public class MainViewModel : DemoViewModelBase { #region 字段 /// /// 内容标题 /// private object _contentTitle; /// /// 子内容 /// private object _subContent; #endregion public MainViewModel(DataService dataService) { Messenger.Default.Register(this, MessageToken.LoadShowContent, obj => { if (SubContent is IDisposable disposable) { disposable.Dispose(); } SubContent = obj; }, true); Messenger.Default.Register(this, MessageToken.ClearLeftSelected, obj => { DemoItemCurrent = null; foreach (var item in DemoInfoCollection) { item.SelectedIndex = -1; } }); Messenger.Default.Register(this, MessageToken.LangUpdated, obj => { if (DemoItemCurrent == null) return; ContentTitle = LangProvider.GetLang(DemoItemCurrent.Name); }); DataList = dataService.GetDemoDataList(); DemoInfoCollection = new ObservableCollection(); #if netle40 Task.Factory.StartNew(() => { foreach (var item in dataService.GetDemoInfo()) { Application.Current.Dispatcher.BeginInvoke(new Action(() => { DemoInfoCollection.Add(item); }), DispatcherPriority.ApplicationIdle); } }); #else Task.Run(() => { foreach (var item in dataService.GetDemoInfo()) { Application.Current.Dispatcher.BeginInvoke(new Action(() => { DemoInfoCollection.Add(item); }), DispatcherPriority.ApplicationIdle); } }); #endif } #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 ObservableCollection DemoInfoCollection { get; set; } #endregion #region 命令 /// /// 切换例子命令 /// public RelayCommand SwitchDemoCmd => new Lazy>(() => new RelayCommand(SwitchDemo)).Value; public RelayCommand OpenPracticalDemoCmd => new Lazy(() => new RelayCommand(OpenPracticalDemo)).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; SwitchDemo(item); } } private void SwitchDemo(DemoItemModel item) { DemoItemCurrent = item; ContentTitle = LangProvider.GetLang(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 OpenPracticalDemo() { Messenger.Default.Send(null, MessageToken.ClearLeftSelected); Messenger.Default.Send(true, MessageToken.FullSwitch); Messenger.Default.Send(AssemblyHelper.CreateInternalInstance($"UserControl.{MessageToken.PracticalDemo}"), MessageToken.LoadShowContent); } #endregion } }