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 HandyControl.Interactivity; 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; private readonly DataService _dataService; #endregion public MainViewModel(DataService dataService) { _dataService = dataService; UpdateMainContent(); UpdateLeftContent(); } #region 属性 /// /// 当前选中的demo项 /// public DemoItemModel DemoItemCurrent { get; private set; } public DemoInfoModel DemoInfoCurrent { get; set; } /// /// 子内容 /// public object SubContent { get => _subContent; #if NET40 set => Set(nameof(SubContent), ref _subContent, value); #else set => Set(ref _subContent, value); #endif } /// /// 内容标题 /// public object ContentTitle { get => _contentTitle; #if NET40 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(SwitchDemo); public RelayCommand OpenPracticalDemoCmd => new(OpenPracticalDemo); public RelayCommand GlobalShortcutInfoCmd => new(() => Growl.Info("Global Shortcut Info")); public RelayCommand GlobalShortcutWarningCmd => new(() => Growl.Warning("Global Shortcut Warning")); public RelayCommand OpenDocCmd => new(() => { ControlCommands.OpenLink.Execute(_dataService.GetDemoUrl(DemoInfoCurrent, DemoItemCurrent)); }); #endregion #region 方法 private void UpdateMainContent() { Messenger.Default.Register(this, MessageToken.LoadShowContent, obj => { if (SubContent is IDisposable disposable) { disposable.Dispose(); } SubContent = obj; }, true); } private void UpdateLeftContent() { //clear status 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); }); //load items DemoInfoCollection = new ObservableCollection(); #if NET40 Task.Factory.StartNew(() => #else Task.Run(() => #endif { DataList = _dataService.GetDemoDataList(); foreach (var item in _dataService.GetDemoInfo()) { Application.Current.Dispatcher.BeginInvoke(new Action(() => { DemoInfoCollection.Add(item); }), DispatcherPriority.ApplicationIdle); } }); } /// /// 切换例子 /// 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(AssemblyHelper.CreateInternalInstance($"UserControl.{MessageToken.PracticalDemo}"), MessageToken.LoadShowContent); Messenger.Default.Send(true, MessageToken.FullSwitch); } #endregion }