123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- using MaterialDesignThemes.Wpf;
- using Prism.Commands;
- using Prism.Events;
- using Prism.Mvvm;
- using Prism.Regions;
- using ProjectManagementSystem.Common.Extenions;
- using ProjectManagementSystem.Common.Function;
- using ProjectManagementSystem.Common.Logger;
- using ProjectManagementSystem.Common.Service;
- using ProjectManagementSystem.Language;
- using ProjectManagementSystem.WebApi.Service;
- using ProjectManagementSystemView.Infrastructure.Events;
- using ProjectManagementSystemView.Infrastructure.Models;
- using PropertyChanged;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Diagnostics;
- using System.Globalization;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows;
- namespace ProjectManagementSystemView.ViewModels
- {
- [AddINotifyPropertyChangedInterface]
- public class MainViewModel
- {
- private readonly IRegionManager regionManager;
- private readonly IEventAggregator eventAggregator;
- private IRegionNavigationJournal journal;
- private string tabPage;
- public string Title { get; set; } = "PMS";
- public string ConnectionIcon { get; set; } = "LanDisconnect";
- public DateTime NowTime { get; set; }
- [OnChangedMethod(nameof(IsDarkThemeChanged))]
- public bool? IsDarkTheme { get; set; }
- [OnChangedMethod(nameof(ChangeCulture))]
- public string CultureInfoName { get; set; }
- public ObservableCollection<MenuBar> MenuBars { get; set; }
- public ObservableCollection<CultureInfo> CultureInfoCollection { get; set; }
- public DelegateCommand LoadedCommand { get; private set; }
- public DelegateCommand<MenuBar> NavigateCommand { get; private set; }
- public DelegateCommand GoBackCommand { get; private set; }
- public DelegateCommand GoForwardCommand { get; private set; }
- public DelegateCommand PushMainWindow2Top { get; private set; }
- public DelegateCommand ShutdownApp { get; private set; }
- public MainViewModel(IRegionManager regionManager, IEventAggregator eventAggregator)
- {
- this.regionManager = regionManager;
- this.eventAggregator = eventAggregator;
- MenuBars = new ObservableCollection<MenuBar>();
- CultureInfoCollection = new ObservableCollection<CultureInfo>();
- CultureInfoCollection.Add(CultureInfo.GetCultureInfo("zh-CN"));
- CultureInfoCollection.Add(CultureInfo.GetCultureInfo("en"));
- var cultureInfo = AppSetting.TryGetValue<string>("CultureInfo");
- var isDarkTheme = AppSetting.TryGetValue<string>(nameof(IsDarkTheme));
- IsDarkTheme = string.IsNullOrWhiteSpace(isDarkTheme) ? true : isDarkTheme.ToValue<bool>();
- CultureInfoName = string.IsNullOrWhiteSpace(cultureInfo) ? CultureInfoCollection.First().Name : cultureInfo;
- LoadedCommand = new DelegateCommand(Init);
- NavigateCommand = new DelegateCommand<MenuBar>(Navigate);
- GoBackCommand = new DelegateCommand(() =>
- {
- if (journal != null && journal.CanGoBack)
- journal.GoBack();
- });
- GoForwardCommand = new DelegateCommand(() =>
- {
- if (journal != null && journal.CanGoForward)
- journal.GoForward();
- });
- PushMainWindow2Top = new DelegateCommand(() =>
- {
- if (!string.IsNullOrEmpty(tabPage))
- {
- if (Application.Current.MainWindow.WindowState == WindowState.Minimized)
- {
- Application.Current.MainWindow.WindowState = WindowState.Normal;
- }
- Application.Current.MainWindow.Visibility = Visibility.Visible;
- Application.Current.MainWindow.Topmost = true;
- Application.Current.MainWindow.Topmost = false;
- }
- });
- ShutdownApp = new DelegateCommand(Shutdown);
- eventAggregator.GetEvent<UpdateViewEvent>().Subscribe(() =>
- {
- ConnectionIcon = Crms.PmsApi.Status ? "LanConnect" : "LanDisconnect";
- NowTime = DateTime.Now;
- });
- }
- private void Init()
- {
- var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
- var dateTime = System.IO.File.GetLastWriteTime(this.GetType().Assembly.Location).ToString("yyyyMMddHHmmss");
- Title = $"PMS{version} ({dateTime})";
- CLog.Instance.SystemLog.WriteInfo($"{Langs.StartingVersion}{Title}");
- var systemEnteranceName = AppSetting.TryGetValue<string>("SystemEnteranceName");
- if (!string.IsNullOrEmpty(systemEnteranceName))
- {
- var assemblyName = this.GetType().Assembly.GetName().Name;
- string fullClassName = $"{assemblyName}.Entrance.{systemEnteranceName}";
- var systemEnterance = InstanceConstructor.GetInstance<object>(assemblyName, fullClassName);
- if (systemEnterance == null)
- {
- MessageBox.Show($"{Langs.CreateInstanceFailed} {systemEnteranceName}");
- Shutdown();
- return;
- }
- }
- CreateMenuBar();
- if (MenuBars.Count > 0)
- {
- Navigate(MenuBars.First());
- Application.Current.MainWindow.WindowState = WindowState.Normal;
- }
- else
- {
- Application.Current.MainWindow.Visibility = Visibility.Collapsed;
- }
- Task.Factory.StartNew(() =>
- {
- CLog.Instance.SystemLog.WriteDebug($"UpdateViewEvent {Langs.Started}");
- while (true)
- {
- try
- {
- eventAggregator.GetEvent<UpdateViewEvent>().Publish();
- }
- catch (Exception ex)
- {
- CLog.Instance.SystemLog.WriteException("UpdateViewEvent", ex);
- Thread.Sleep(5000);
- }
- Thread.Sleep(1000);
- }
- }, TaskCreationOptions.LongRunning);
- CLog.Instance.SystemLog.WriteInfo(Langs.StartCompleted);
- }
- private void Navigate(MenuBar obj)
- {
- if (obj == null || string.IsNullOrWhiteSpace(obj.NameSpace)) return;
- if (!regionManager.Regions.ContainsRegionWithName("MainViewRegion")) return;
- regionManager.Regions["MainViewRegion"]?.RequestNavigate(obj.NameSpace, back =>
- {
- journal = back.Context.NavigationService.Journal;
- });
- }
- private void CreateMenuBar()
- {
- tabPage = AppSetting.TryGetValue<string>("TabPage");
- string[] tabPageArray = tabPage.ToValueArray<string>();
- for (int i = 0; i < tabPageArray.Length; i++)
- {
- string pagInfoStr = tabPageArray[i];
- string[] pageInfos = pagInfoStr.ToValueArray<string>(':');
- string page = pageInfos[0];
- //string pageName = pageInfos.Length > 1 ? pageInfos[1] : pageInfos[0];
- string pageName = ResourceService.GetString(page);
- MenuBars.Add(new MenuBar() { Title = pageName, NameSpace = page });
- }
- }
- private void Shutdown()
- {
- WebApiServerHost.Instance.CloseServer();
- Application.Current.Shutdown();
- Process[] process = Process.GetProcesses();
- Process currentProcess = Process.GetCurrentProcess();
- for (int i = 0; i < process.Count(); i++)
- {
- if (process[i].ProcessName == currentProcess.ProcessName)
- {
- process[i].Kill();
- }
- }
- }
- private void ChangeCulture()
- {
- try
- {
- var langName = CultureInfoName;
- ResourceService.Current.ChangeCulture(langName);
- foreach (var item in MenuBars)
- {
- var title = ResourceService.GetString(item.NameSpace);
- if (!string.IsNullOrWhiteSpace(title))
- {
- item.Title = title;
- }
- }
- var cultureInfo = AppSetting.TryGetValue<string>("CultureInfo");
- if (cultureInfo != langName)
- {
- AppSetting.TrySaveValue("CultureInfo", langName);
- }
- }
- catch (Exception ex)
- {
- CLog.Instance.SystemLog.WriteException("ChangeCulture", ex);
- }
- }
- private void IsDarkThemeChanged()
- {
- try
- {
- var paletteHelper = new PaletteHelper();
- var theme = paletteHelper.GetTheme();
- theme.SetBaseTheme(IsDarkTheme == true ? Theme.Dark : Theme.Light);
- paletteHelper.SetTheme(theme);
- var isDarkTheme = AppSetting.TryGetValue<bool>(nameof(IsDarkTheme));
- if (isDarkTheme != IsDarkTheme)
- {
- AppSetting.TrySaveValue(nameof(IsDarkTheme), IsDarkTheme.ToString());
- }
- }
- catch (Exception ex)
- {
- CLog.Instance.SystemLog.WriteException("IsDarkThemeChanged", ex);
- }
- }
- }
- }
|