diff --git a/GeneralServiceHost/App.xaml.cs b/GeneralServiceHost/App.xaml.cs index a4591bf..761d3ed 100644 --- a/GeneralServiceHost/App.xaml.cs +++ b/GeneralServiceHost/App.xaml.cs @@ -12,6 +12,7 @@ using GeneralServiceHost.Common; using GeneralServiceHost.Helper; using GeneralServiceHost.Model; +using GeneralServiceHost.View; using GeneralServiceHost.ViewModel; using Microsoft.Extensions.DependencyInjection; @@ -33,6 +34,7 @@ public App() //ViewModels .AddSingleton() .AddSingleton() + .AddScoped() .BuildServiceProvider()); diff --git a/GeneralServiceHost/MainWindow.xaml.cs b/GeneralServiceHost/MainWindow.xaml.cs index 2752b18..0757fcd 100644 --- a/GeneralServiceHost/MainWindow.xaml.cs +++ b/GeneralServiceHost/MainWindow.xaml.cs @@ -15,6 +15,7 @@ using System.Windows.Shapes; using GeneralServiceHost.View; using MahApps.Metro.Controls; +using MahApps.Metro.Controls.Dialogs; using Microsoft.Win32; namespace GeneralServiceHost @@ -72,12 +73,23 @@ private void MetroWindow_Closing(object sender, System.ComponentModel.CancelEven { return; } - MessageBoxResult result = System.Windows.MessageBox.Show("是否退出GSH?否则最小化到任务栏,取消则返回。", "提示", MessageBoxButton.YesNoCancel, MessageBoxImage.Warning, MessageBoxResult.No); - if (result == MessageBoxResult.Cancel) + + var settings = new MetroDialogSettings() + { + AffirmativeButtonText = "退出程序", + NegativeButtonText = "最小化至托盘", + FirstAuxiliaryButtonText = "取消", + ColorScheme = this.MetroDialogOptions!.ColorScheme + }; + + MessageDialogResult result = this.ShowModalMessageExternal("提示", "是否退出GSH", + MessageDialogStyle.AffirmativeAndNegativeAndSingleAuxiliary, settings); + + if (result == MessageDialogResult.FirstAuxiliary) { e.Cancel = true; } - else if (result == MessageBoxResult.No) + else if (result == MessageDialogResult.Negative) { e.Cancel = true; this.ShowInTaskbar=false; diff --git a/GeneralServiceHost/View/IndexPage.xaml.cs b/GeneralServiceHost/View/IndexPage.xaml.cs index 9f81b49..95427a2 100644 --- a/GeneralServiceHost/View/IndexPage.xaml.cs +++ b/GeneralServiceHost/View/IndexPage.xaml.cs @@ -14,9 +14,12 @@ using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; +using CommunityToolkit.Mvvm.DependencyInjection; using FluentScheduler; using GeneralServiceHost.Manager; using GeneralServiceHost.Model; +using GeneralServiceHost.ViewModel; +using Microsoft.Extensions.DependencyInjection; using Schedule = FluentScheduler.Schedule; namespace GeneralServiceHost.View @@ -34,8 +37,15 @@ public IndexPage() private void ButtonBase_OnClick(object sender, RoutedEventArgs e) { - var addJobWindow = new AddJobWindow(); - addJobWindow.ShowDialog(); + IServiceScopeFactory _serviceScopeFactory = Ioc.Default.GetRequiredService(); + using (IServiceScope scope = _serviceScopeFactory.CreateScope()) + { + var addJobWindow = scope.ServiceProvider.GetRequiredService(); + Ioc.Default.GetRequiredService().Scope=scope; + addJobWindow.ShowDialog(); + } + + } private void FrameworkElement_OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e) diff --git a/GeneralServiceHost/ViewModel/AddJobWindowViewModel.cs b/GeneralServiceHost/ViewModel/AddJobWindowViewModel.cs index e6f34fd..e34ffe5 100644 --- a/GeneralServiceHost/ViewModel/AddJobWindowViewModel.cs +++ b/GeneralServiceHost/ViewModel/AddJobWindowViewModel.cs @@ -2,13 +2,18 @@ using System.ComponentModel; using System.Reflection; using System.Windows; - +using CommunityToolkit.Mvvm.DependencyInjection; using CommunityToolkit.Mvvm.Input; using CommunityToolkit.Mvvm.Messaging; using GeneralServiceHost.Common; +using GeneralServiceHost.Helper; using GeneralServiceHost.Manager; using GeneralServiceHost.Model; +using GeneralServiceHost.View; +using MahApps.Metro.Controls; +using MahApps.Metro.Controls.Dialogs; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Win32; namespace GeneralServiceHost.ViewModel @@ -23,7 +28,6 @@ public AddJobWindowViewModel() UploadFileCommand = new RelayCommand(UploadFileAction); ContinuallyModeSelectedCommand = new RelayCommand(ContinuallyModeSelectedAction); //this.ScheduleInfo = new ScheduleInfo() { Name = "程序集选择完成后显示" }; - this.PropertyChanged += AddJobWindowViewModel_PropertyChanged; } private void ContinuallyModeSelectedAction() @@ -32,35 +36,18 @@ private void ContinuallyModeSelectedAction() this.ScheduleInfo.IsToRunNow = true; } - private void AddJobWindowViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e) - { - if (e.PropertyName == nameof(Asm)) - { - if (Asm != null) - { - this.ScheduleInfo.AsmPath = Asm.Location; - this.ScheduleInfo.AsmName = Asm.GetName().Name; - - } - } - } private void UploadFileAction() { ChoiceDLL(); - if (Asm != null) - { - this.ScheduleInfo.Name = Asm.GetName().Name; - } - } private bool ValidateSchedule() { if (string.IsNullOrEmpty(this.ScheduleInfo.AsmPath)) { - MessageBox.Show("请指定要运行的程序", "信息不完整",MessageBoxButton.OK,MessageBoxImage.Information); + MessageBox.Show("请指定要运行的程序", "信息不完整", MessageBoxButton.OK, MessageBoxImage.Information); return false; } @@ -109,19 +96,8 @@ private void SetAction() public RelayCommand UploadFileCommand { get; set; } - private Assembly _asm; - - public Assembly Asm - { - get { return _asm; } - set - { - _asm = value; - OnPropertyChanged(nameof(Asm)); - } - } - + public IServiceScope Scope { get; set; } private ScheduleInfo _scheduleInfo; @@ -144,7 +120,7 @@ public ScheduleInfo ScheduleInfo } - public void ChoiceDLL() + public async void ChoiceDLL() { var dialog = new OpenFileDialog() { @@ -156,7 +132,9 @@ public void ChoiceDLL() var exe = dialog.FileName; try { - Asm = Assembly.LoadFrom(exe); + var Asm = Assembly.LoadFrom(exe); + this.ScheduleInfo.AsmPath = Asm.Location; + this.ScheduleInfo.AsmName = Asm.GetName().Name; } catch (BadImageFormatException ex) { @@ -164,7 +142,36 @@ public void ChoiceDLL() if (errorCode == COR_E_ASSEMBLYEXPECTED) { MessageBox.Show("此exe文件无法在当前的Windows中运行"); + return; + } + var dllpath = exe.Replace(".exe", ".dll"); + if (DirFileHelper.IsExistFile(dllpath)) + { + try + { + var dllasm = Assembly.LoadFrom(dllpath); + this.ScheduleInfo.AsmPath = exe; + this.ScheduleInfo.AsmName = dllasm.GetName().Name; + this.ScheduleInfo.Name = dllasm.GetName().Name; + } + catch (BadImageFormatException ex2) + { + MessageBox.Show("此exe文件无法在当前的Windows中运行"); + return; + } + + } + else + { + + + var name = await DialogManager.ShowInputAsync(this.Scope.ServiceProvider.GetRequiredService(), "输入名称", "无法读取当前程序集,请指定一个名称"); + + this.ScheduleInfo.AsmPath = exe; + this.ScheduleInfo.AsmName = name; + this.ScheduleInfo.Name = name; } + }