添加项目文件。
This commit is contained in:
94
App.xaml.cs
Normal file
94
App.xaml.cs
Normal file
@@ -0,0 +1,94 @@
|
||||
using HME_MoistureLossMeter.Models;
|
||||
using HME_MoistureLossMeter.Services;
|
||||
using HME_MoistureLossMeter.ViewModels;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Serilog;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Windows;
|
||||
|
||||
namespace HME_MoistureLossMeter
|
||||
{
|
||||
public partial class App : Application
|
||||
{
|
||||
private IHost _host;
|
||||
public static IServiceProvider ServiceProvider { get; private set; }
|
||||
|
||||
protected override void OnStartup(StartupEventArgs e)
|
||||
{
|
||||
base.OnStartup(e);
|
||||
|
||||
// 配置日志
|
||||
Log.Logger = new LoggerConfiguration()
|
||||
.MinimumLevel.Information()
|
||||
.WriteTo.File(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs", "log-.txt"),
|
||||
rollingInterval: RollingInterval.Day,
|
||||
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] {Message:lj}{NewLine}{Exception}")
|
||||
.CreateLogger();
|
||||
|
||||
try
|
||||
{
|
||||
_host = Host.CreateDefaultBuilder(e.Args)
|
||||
.ConfigureServices((context, services) =>
|
||||
{
|
||||
// 配置
|
||||
var plcConfig = context.Configuration.GetSection("PlcConfiguration").Get<PlcConfiguration>();
|
||||
var mesConfig = context.Configuration.GetSection("MesConfiguration").Get<MesConfiguration>();
|
||||
var deviceConfig = context.Configuration.GetSection("DeviceConfiguration").Get<DeviceConfiguration>();
|
||||
|
||||
services.AddSingleton(plcConfig);
|
||||
services.AddSingleton(mesConfig);
|
||||
services.AddSingleton(deviceConfig);
|
||||
|
||||
// 服务
|
||||
services.AddSingleton<IPlcService, ModbusTcpPlcService>();
|
||||
services.AddSingleton<IMesService, MesHttpService>();
|
||||
|
||||
// ViewModels
|
||||
services.AddSingleton<MainViewModel>();
|
||||
services.AddSingleton<TestViewModel>();
|
||||
services.AddSingleton<ManualControlViewModel>();
|
||||
services.AddSingleton<RecordViewModel>();
|
||||
services.AddSingleton<ReportViewModel>();
|
||||
|
||||
|
||||
// HttpClient
|
||||
services.AddHttpClient<IMesService, MesHttpService>(client =>
|
||||
{
|
||||
client.BaseAddress = new Uri(mesConfig.BaseUrl);
|
||||
client.Timeout = TimeSpan.FromSeconds(mesConfig.TimeoutSeconds);
|
||||
});
|
||||
})
|
||||
.UseSerilog()
|
||||
.Build();
|
||||
|
||||
ServiceProvider = _host.Services;
|
||||
|
||||
// 初始化并启动服务
|
||||
var plcService = ServiceProvider.GetRequiredService<IPlcService>();
|
||||
var mesService = ServiceProvider.GetRequiredService<IMesService>();
|
||||
|
||||
// 创建主窗口
|
||||
var mainWindow = new MainWindow();
|
||||
mainWindow.DataContext = ServiceProvider.GetRequiredService<MainViewModel>();
|
||||
mainWindow.Show();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Fatal(ex, "应用程序启动失败");
|
||||
MessageBox.Show($"启动失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
Shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnExit(ExitEventArgs e)
|
||||
{
|
||||
Log.Information("应用程序关闭");
|
||||
Log.CloseAndFlush();
|
||||
_host?.Dispose();
|
||||
base.OnExit(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user