From 1f8ac078686ee7759116cf1aec74c4f9210b3aa3 Mon Sep 17 00:00:00 2001 From: tanglong <842690096@qq.com> Date: Wed, 21 Jan 2026 14:48:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=B3=E9=97=AD=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Wpf_AiSportsMicrospace/App.xaml.cs | 91 ++------------------ Wpf_AiSportsMicrospace/Common/AppSettings.cs | 4 +- Wpf_AiSportsMicrospace/MainWindow.xaml.cs | 4 +- Wpf_AiSportsMicrospace/Views/Main.xaml.cs | 2 +- 4 files changed, 11 insertions(+), 90 deletions(-) diff --git a/Wpf_AiSportsMicrospace/App.xaml.cs b/Wpf_AiSportsMicrospace/App.xaml.cs index 43b6688..396b450 100644 --- a/Wpf_AiSportsMicrospace/App.xaml.cs +++ b/Wpf_AiSportsMicrospace/App.xaml.cs @@ -32,9 +32,7 @@ public partial class App : Application DebugTracker.Channels.Add(new DiagnosisDebugTrackChannel()); #endif - base.OnStartup(e); - - // 创建 Host + // 1. 创建 Host AppHost = Host.CreateDefaultBuilder() .ConfigureAppConfiguration((context, config) => { @@ -48,81 +46,19 @@ public partial class App : Application services.AddDbContext(options => options.UseSqlServer(connectionString)); - // 初始化全局配置 - AppSettings.Init(context.Configuration); - // 注入 MainWindow services.AddSingleton
(); - - var SSS = AppSettings.SchoolCode; }) .Build(); - // 启动 Host + // 2. 启动 Host AppHost.Start(); - //using (var scope = AppHost.Services.CreateScope()) - //{ - // var db = scope.ServiceProvider.GetRequiredService(); - // //db.Database.EnsureDeleted(); // 删除数据库和表 - // db.Database.EnsureCreated(); - // //db.Database.ExecuteSqlRaw("DROP TABLE [JumpLongPoints]"); - // if (!db.SixGroupPoints.Any()) - // { - // db.SixGroupPoints.Add(new AiSportsMicrospaceDB.Entities.SixGroupPoints - // { - // Id = 1, - // Point1 = new AiSportsMicrospaceDB.Entities.Point { X = 0.21, Y = 0.88 }, - // Point2 = new AiSportsMicrospaceDB.Entities.Point { X = 0.36, Y = 0.58 }, - // Point3 = new AiSportsMicrospaceDB.Entities.Point { X = 0.50, Y = 0.88 }, - // Point4 = new AiSportsMicrospaceDB.Entities.Point { X = 0.64, Y = 0.58 }, - // Point5 = new AiSportsMicrospaceDB.Entities.Point { X = 0.78, Y = 0.88 }, - // Point6 = new AiSportsMicrospaceDB.Entities.Point { X = 0.92, Y = 0.58 } - // }); - // db.SaveChanges(); - // } - - // //if (!CheckTableExists(db , "JumpLongPoints")) - // //{ - // // var createTableSql = @" - // // CREATE TABLE IF NOT EXISTS JumpLongPoints ( - // // Id INTEGER PRIMARY KEY, - // // TopPoint1_X TEXT NOT NULL, - // // TopPoint2_X TEXT NOT NULL, - // // TopPoint3_X TEXT NOT NULL, - // // BottomPoint1_X TEXT NOT NULL, - // // BottomPoint2_X TEXT NOT NULL, - // // BottomPoint3_X TEXT NOT NULL, - // // TopPoint1_Y TEXT NOT NULL, - // // TopPoint2_Y TEXT NOT NULL, - // // TopPoint3_Y TEXT NOT NULL, - // // BottomPoint1_Y TEXT NOT NULL, - // // BottomPoint2_Y TEXT NOT NULL, - // // BottomPoint3_Y TEXT NOT NULL - // // )"; - - // // db.Database.ExecuteSqlRaw(createTableSql); - // //} - - // if (!db.JumpLongPoints.Any()) - // { - // db.JumpLongPoints.Add(new AiSportsMicrospaceDB.Entities.JumpLongPoints - // { - // Id = 1, - // TopPoint1 = new AiSportsMicrospaceDB.Entities.Point { X = 245, Y = 741 }, - // TopPoint2 = new AiSportsMicrospaceDB.Entities.Point { X = 1498, Y = 730 }, - // TopPoint3 = new AiSportsMicrospaceDB.Entities.Point { X = 1689, Y = 724 }, - // BottomPoint1 = new AiSportsMicrospaceDB.Entities.Point { X = 191, Y = 918 }, - // BottomPoint3 = new AiSportsMicrospaceDB.Entities.Point { X = 1722, Y = 909 }, - // }); - // db.SaveChanges(); - // } - // //var mainWindow = new MainWindow(db); - // //mainWindow.Show(); - //} - - // 用 DI 创建窗口 + // 3. 初始化全局配置 + var configuration = AppHost.Services.GetRequiredService(); + AppSettings.Init(configuration); // 这时 AppHost 已经有值了 + // 4. 用 DI 创建窗口 var mainWin = AppHost.Services.GetRequiredService
(); Current.MainWindow = mainWin; mainWin.Show(); @@ -131,20 +67,5 @@ public partial class App : Application mainWin.SwitchPage(newPage, true); } - - //private bool CheckTableExists(AppDbContext dbContext , String name) - //{ - // try - // { - // // 使用原始SQL检查表是否存在 - // var sql = "SELECT count(*) FROM sqlite_master WHERE type='table' AND name=@name"; - // var result = dbContext.Database.ExecuteSqlRaw(sql, new SqlServerParameter("@name", name)); - // return result > 0; - // } - // catch - // { - // return false; - // } - //} } diff --git a/Wpf_AiSportsMicrospace/Common/AppSettings.cs b/Wpf_AiSportsMicrospace/Common/AppSettings.cs index e12d4f6..ba60cad 100644 --- a/Wpf_AiSportsMicrospace/Common/AppSettings.cs +++ b/Wpf_AiSportsMicrospace/Common/AppSettings.cs @@ -33,9 +33,9 @@ namespace Common using var scope = App.AppHost.Services.CreateScope(); var db = scope.ServiceProvider.GetRequiredService(); - var groupData = db.GroupPoints.Where(x => x.Code == Code).Select(x => new { x.ControlPoint, x.AreaSize }).FirstOrDefault(); + var groupData = db.GroupPoints.Where(x => x.Code == Code).FirstOrDefault(); - if (groupData != null && groupData.ControlPoint != null && groupData.AreaSize != null) + if (groupData != null) { ControlPoint = groupData.ControlPoint; AreaSize = groupData.AreaSize; diff --git a/Wpf_AiSportsMicrospace/MainWindow.xaml.cs b/Wpf_AiSportsMicrospace/MainWindow.xaml.cs index 590c6ba..bb66711 100644 --- a/Wpf_AiSportsMicrospace/MainWindow.xaml.cs +++ b/Wpf_AiSportsMicrospace/MainWindow.xaml.cs @@ -106,7 +106,7 @@ public partial class MainWindow : Window }); // UI 线程显示 - Application.Current.Dispatcher.Invoke(() => + this.Dispatcher.BeginInvoke(() => { if (videoImage == null) return; @@ -138,7 +138,7 @@ public partial class MainWindow : Window var circlePositions = GenerateCirclePositionsStaggered( roomWidth: 5, // 房间宽度(米) roomHeight: 3, // 房间高度(米) - yOffset: 0.6, // 前排纵坐标 + yOffset: 0.6, // 前排纵坐 yStep: 1.5, // 前后排间距 minDistance: 1.5, // 同排最小间距 minEdge: 1 // 两侧到边缘最小距离 diff --git a/Wpf_AiSportsMicrospace/Views/Main.xaml.cs b/Wpf_AiSportsMicrospace/Views/Main.xaml.cs index 74f57ab..7af21af 100644 --- a/Wpf_AiSportsMicrospace/Views/Main.xaml.cs +++ b/Wpf_AiSportsMicrospace/Views/Main.xaml.cs @@ -109,7 +109,7 @@ namespace Wpf_AiSportsMicrospace.Views return; // 触发全局事件 - Application.Current.Dispatcher.BeginInvoke(() => + this.Dispatcher.BeginInvoke(() => { HumanFrameUpdated?.Invoke(this, humans); });