BOB/ProcessManager/ViewModels/PMainViewModel.cs
2025-11-17 17:19:46 +08:00

84 lines
2.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection.Metadata;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace ProcessManager.ViewModels
{
public class PMainViewModel:BindableBase
{
private readonly IDialogService _dialogService;
private Dictionary<string, Process> ProcessDic { get; set; } = new Dictionary<string, Process>();
public ICommand StartCommand { get; set; }
public ICommand EditCommand { get; set; }
public PMainViewModel(IDialogService dialogService)
{
_dialogService = dialogService;
EditCommand=new DelegateCommand<string>(OnEdit);
StartCommand = new DelegateCommand<string>(OnStart);
}
private void OnStart(string deviceName)
{
if (ProcessDic.ContainsKey(deviceName))
{
MessageBox.Show("进程已启动");
return;
}
try
{
string currentDir = Environment.CurrentDirectory;
string exePath = Path.Combine(currentDir, "BOB.exe");
if (!File.Exists(exePath))
{
MessageBox.Show($"未找到文件: {exePath}");
return;
}
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = exePath,
UseShellExecute = true
};
var process = Process.Start(startInfo);
process.EnableRaisingEvents = true;
process.Exited += (s, args) =>
{
ProcessDic.Remove(deviceName);
};
ProcessDic.Add(deviceName, process);
}
catch (Exception ex)
{
MessageBox.Show($"启动失败: {ex.Message}");
}
}
private void OnEdit(string deviceName)
{
if (ProcessDic.ContainsKey(deviceName))
{
MessageBox.Show("请先关闭进程再进行编辑");
return;
}
var ConfigSystem = JsonHelper.LoadFromFile();
var parameters = new DialogParameters
{
{ "SystemConfig", ConfigSystem },
{ "Title", deviceName }
};
_dialogService.ShowDialog("Setting", parameters, r =>
{
if (r.Result == ButtonResult.OK)
{
// 用户点击 OK
}
});
}
}
}