Skip to content

Commit

Permalink
bug fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
jevonsflash committed Jul 26, 2022
1 parent 05857d6 commit df94ad3
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 39 deletions.
2 changes: 2 additions & 0 deletions GeneralServiceHost/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using GeneralServiceHost.Common;
using GeneralServiceHost.Helper;
using GeneralServiceHost.Model;
using GeneralServiceHost.View;
using GeneralServiceHost.ViewModel;
using Microsoft.Extensions.DependencyInjection;

Expand All @@ -33,6 +34,7 @@ public App()
//ViewModels
.AddSingleton<IndexPageViewModel>()
.AddSingleton<AddJobWindowViewModel>()
.AddScoped<AddJobWindow>()
.BuildServiceProvider());


Expand Down
18 changes: 15 additions & 3 deletions GeneralServiceHost/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
14 changes: 12 additions & 2 deletions GeneralServiceHost/View/IndexPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<IServiceScopeFactory>();
using (IServiceScope scope = _serviceScopeFactory.CreateScope())
{
var addJobWindow = scope.ServiceProvider.GetRequiredService<AddJobWindow>();
Ioc.Default.GetRequiredService<AddJobWindowViewModel>().Scope=scope;
addJobWindow.ShowDialog();
}


}

private void FrameworkElement_OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
Expand Down
75 changes: 41 additions & 34 deletions GeneralServiceHost/ViewModel/AddJobWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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;

}
Expand Down Expand Up @@ -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;

Expand All @@ -144,7 +120,7 @@ public ScheduleInfo ScheduleInfo
}


public void ChoiceDLL()
public async void ChoiceDLL()
{
var dialog = new OpenFileDialog()
{
Expand All @@ -156,15 +132,46 @@ 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)
{
int errorCode = System.Runtime.InteropServices.Marshal.GetHRForException(ex);
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<AddJobWindow>(), "输入名称", "无法读取当前程序集,请指定一个名称");

this.ScheduleInfo.AsmPath = exe;
this.ScheduleInfo.AsmName = name;
this.ScheduleInfo.Name = name;
}

}


Expand Down

0 comments on commit df94ad3

Please sign in to comment.