123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- using Avalonia;
- using Avalonia.Controls;
- using Avalonia.Controls.ApplicationLifetimes;
- using CommunityToolkit.Mvvm.Input;
- using IModel;
- using IViewModel.ViewModels;
- using IViewModel.Views;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Runtime.CompilerServices;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Input;
- namespace Dynamicloadsimulationdevice.ViewModels
- {
- internal sealed class MainWindowViewModel:IViewModel.ViewModels.DisplayViewModelBase
- {
- private Dictionary<RuntimeTypeHandle, BaseDialogWindow> opendWindows = new Dictionary<RuntimeTypeHandle, BaseDialogWindow>();
- private List<ViewModelBase> AllViewModels = new List<ViewModelBase>();
- private readonly string PluginPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory+"Plugins");
- private MainWindowViewModel()
- {
- IViewModel.ViewModels.LanguageViewModel.Instance.Init();
- MenuViewModel.Instance.MenuClick += (sender, args) =>
- {
- switch(args!.Header)
- {
- case nameof(LanguageValueViewModel.MenuExit):
- Exit();
- break;
- case nameof(LanguageValueViewModel.MenuAbout):
- break;
- default:
- {
- var v = AllViewModels.OfType<IDisplayViewModel>().FirstOrDefault(x => x.MenuKey == args.Header);
- if(v==null)
- {
- ShowToast(LanguageValueViewModel.Instance.NotSupport, Avalonia.Controls.Notifications.NotificationType.Error);
- return;
- }
- if(v.ShowTop)
- {
- if (App.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
- {
- IViewModel.Views.BaseDialogWindow window = new IViewModel.Views.BaseDialogWindow();
- window.DataContext = v;
- v.InitData();
- window.ShowDialog(desktop.MainWindow!);
- }
- }
- else
- {
- var handle = v.GetType().TypeHandle;
- if(opendWindows.TryGetValue(handle,out var window))
- {
- window.Activate();
- }
- else
- {
- window = new IViewModel.Views.BaseDialogWindow();
- window.DataContext = v;
- opendWindows[handle] = window;
- window.Closed += (_, _) =>
- {
- opendWindows.Remove(handle);
- };
- v.InitData();
- window.Show();
- }
- }
- }
- break;
- }
- };
- OilSourceControl =IModel.Tools.PluginsLoader.Defalut.Load<IOilSourceControl.IOilSourceControl>(PluginPath).FirstOrDefault();
- if(OilSourceControl!=null)
- {
- OilSourceControl.LocalCommunication = () => CommunicationViewModel.Instance.LocalCommunication;
- OilSourceControl.RemoteCommunication = () => CommunicationViewModel.Instance.ServiceCommunication;
- }
- AllViewModels.AddRange(System.Runtime.Loader.AssemblyLoadContext.Default.Assemblies.SelectMany(x => GetAssemblyViewModels(x)));
- AllViewModels.OfType<IDisplayViewModel>().Where(x => x.ShowMenu && !string.IsNullOrEmpty(x.MenuParentKey))
- .GroupBy(x => x.MenuParentKey)
- .ToList()
- .ForEach(x=>
- {
- var menuItem = MenuViewModel.Instance[x.Key];
- if(menuItem ==null)
- {
- menuItem = new MenuItemViewModel();
- menuItem.Header = x.Key;
- MenuViewModel.Instance.Menus.Add(menuItem);
- }
- x.Select(y=>
- {
- return(y.GetType().GetCustomAttribute<MenuOrderAttribute>()?.Order?? int.MinValue,y);
- })
- .OrderBy(x=>x.Item1)
- .ToList().ForEach(y =>
- {
- if(menuItem.Items.Count>0 && y.y.AppendSeparator)
- {
- menuItem.Items.Add(new MenuItemViewModel()
- {
- IsSeparator = true,
- });
- }
- menuItem.Items.Add(new MenuItemViewModel()
- {
- Header = y.y.MenuKey,
- IconKey = y.y.IconKey,
- });
- });
- });
- }
- static MainWindowViewModel()
- {
- }
- public override void Init()
- {
- }
- private bool debug = false;
- public bool Debug { get=>debug; set=> SetProperty(ref debug, value); }
- public IOilSourceControl.IOilSourceControl? OilSourceControl { get; private set; }
- public ICommand ClosingCommand =>new RelayCommand<WindowClosingEventArgs>(Closing);
- private void Closing(object? sender, WindowClosingEventArgs? args)
- {
- if (args != null)
- {
- args.Cancel = true;
- Exit();
- }
- }
- private void Exit()
- {
- ShowAsk(LanguageValueViewModel.Instance.PromptExitMsg,()=>
- {
- Environment.Exit(0);
- });
- }
- public static MainWindowViewModel Instance { get; } = new MainWindowViewModel();
- private List<ViewModelBase> GetAssemblyViewModels(Assembly assembly)
- {
- List<ViewModelBase> vm = new List<ViewModelBase>();
- if (assembly == null) return new List<ViewModelBase>();
- return assembly.GetTypes()
- .Where(x => x.IsAssignableTo(typeof(ViewModelBase)) && x.IsAnsiClass && !x.IsAbstract && x!= typeof(MainWindowViewModel))
- .Where(x =>
- {
- var pro = x.GetProperty("Instance", BindingFlags.Static | BindingFlags.GetProperty | BindingFlags.Public);
- if (pro == null) return false;
- return pro.PropertyType == x;
- })
- .Select(x =>
- {
- var pro = x.GetProperty("Instance", BindingFlags.Static | BindingFlags.GetProperty | BindingFlags.Public);
- var val = pro?.GetValue(null);
- if (val is ViewModelBase vm) return vm;
- return null;
- })
- .Where(x => x != null)
- .Select(x=>x!)
- .ToList();
-
- }
- }
- }
|