Files

72 lines
2.1 KiB
C#
Raw Permalink Normal View History

2026-05-15 20:39:11 +08:00
using ASTM_D7896_Tester.Models;
using ASTM_D7896_Tester.Services;
using ASTM_D7896_Tester.Views;
using Microsoft.Extensions.Configuration;
using OfficeOpenXml;
using System;
using System.Configuration;
2026-04-18 19:00:34 +08:00
using System.Data;
2026-05-15 20:39:11 +08:00
using System.IO;
2026-04-18 19:00:34 +08:00
using System.Windows;
2026-05-15 20:39:11 +08:00
namespace ASTM_D7896_Tester
2026-04-18 19:00:34 +08:00
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
2026-05-15 20:39:11 +08:00
public static IPlcService PlcService { get; private set; }
public static AppConfig PlcConfig { get; private set; }
protected override async void OnStartup(StartupEventArgs e)
{
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
base.OnStartup(e);
// 防止在登录窗口关闭时应用程序因没有窗口而自动退出
ShutdownMode = ShutdownMode.OnExplicitShutdown;
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
var configuration = builder.Build();
// appsettings.json in this project keeps PLC settings at the root of the file
// (not under a "PlcSettings" section). Bind the entire configuration to AppConfig.
PlcConfig = configuration.Get<AppConfig>() ?? new AppConfig();
PlcService = new PlcService(PlcConfig);
var plcService = App.PlcService as PlcService;
try
{
await plcService.EnsureConnectedAsync();
}
catch (Exception ex)
{
MessageBox.Show($"PLC 连接失败:{ex.Message}");
}
// 启动主窗口,设置为应用程序的主窗口并恢复默认的退出模式
var mainWindow = new MainWindow();
MainWindow = mainWindow;
ShutdownMode = ShutdownMode.OnMainWindowClose;
mainWindow.Show();
}
protected override void OnExit(ExitEventArgs e)
{
(PlcService as IDisposable)?.Dispose();
base.OnExit(e);
}
2026-04-18 19:00:34 +08:00
}
}