2025-11-28 09:12:46 +08:00

143 lines
5.2 KiB
C#

using AiSportsMicrospaceDB.DBContext;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.ML.Runtime;
using System.Configuration;
using System.Data;
using System.IO;
using System.Windows;
using Wpf_AiSportsMicrospace.Views;
using Yztob.AiSports.Common;
using Yztob.AiSports.Common.Implement;
namespace Wpf_AiSportsMicrospace;
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
public static Microsoft.Extensions.Hosting.IHost AppHost { get; private set; }
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
#if DEBUG
DebugTracker.Enabled = true;
DebugTracker.Channels.Add(new DiagnosisDebugTrackChannel());
#endif
AppHost = Host.CreateDefaultBuilder()
.ConfigureAppConfiguration((context, config) =>
{
config.SetBasePath(Directory.GetCurrentDirectory());
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
})
.ConfigureServices((context, services) =>
{
var connectionString = context.Configuration.GetConnectionString("DefaultConnection");
services.AddDbContext<AppDbContext>(options =>
options.UseSqlite(connectionString));
//services.AddSingleton<MainWindow>();
services.AddSingleton<Main>();
})
.Build();
AppHost.Start();
using (var scope = AppHost.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
//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 创建窗口
var mainWin = AppHost.Services.GetRequiredService<Main>();
Current.MainWindow = mainWin;
mainWin.Show();
var newPage = new Home();
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 SqliteParameter("@name", name));
return result > 0;
}
catch
{
return false;
}
}
}