Merge pull request #220 from niuzhix/dev-3.0

feat: 添加启动页面
This commit is contained in:
denglihong2007
2025-08-06 21:29:55 +08:00
committed by GitHub
10 changed files with 169 additions and 19 deletions

View File

@@ -9,6 +9,7 @@ namespace CRSim.Core.Abstractions
{
public interface ISettingsService
{
void LoadSettings();
void SaveSettings();
Settings GetSettings();
}

View File

@@ -11,11 +11,6 @@ namespace CRSim.Core.Services
private List<Station> _stations;
private List<TrainNumber> _trainNumbers;
public DatabaseService()
{
ImportData(AppPaths.ConfigFilePath);
}
public List<Station> GetAllStations()
{
return _stations;

View File

@@ -9,10 +9,6 @@ namespace CRSim.Core.Services
{
private Settings _settings;
private RegistryKey _key = Registry.CurrentUser.OpenSubKey(@"Software\CRSim\Settings",true);
public SettingsService()
{
LoadSettings();
}
public void SaveSettings()
{
_key.SetValue("SwitchPageSeconds", _settings.SwitchPageSeconds);
@@ -27,7 +23,7 @@ namespace CRSim.Core.Services
_key.SetValue("LoadTodayOnly", _settings.LoadTodayOnly);
_key.SetValue("ReopenUnclosedScreensOnLoad", _settings.ReopenUnclosedScreensOnLoad);
}
private void LoadSettings()
public void LoadSettings()
{
if (_key==null)
{

View File

@@ -5,11 +5,13 @@
public static IHost AppHost { get; private set; }
public static MainWindow MainWindow;
public string[] commandLineArgs;
public CommandLineOptions parsedOptions;
public static string AppVersion { get; set; } = Assembly.GetAssembly(typeof(App)).GetName().Version.ToString();
public App()
{
var args = Environment.GetCommandLineArgs();
var parsedOptions = CommandLineParser.Parse(args);
parsedOptions = CommandLineParser.Parse(args);
if (parsedOptions.Debug)
{
LaunchDebugConsole();
@@ -27,25 +29,64 @@
services.AddSingleton<IDatabaseService, DatabaseService>();
services.AddSingleton<INetworkService, NetworkService>();
services.AddSingleton<IDialogService, DialogService>();
services.AddTransient<ITimeService, TimeService>();
services.AddSingleton<StyleManager>();
services.AddSingleton<MainWindow>();
services.AddTransient<ITimeService, TimeService>();
services.AddSingleton<StartWindow>();
services.AddTransient<DashboardPageViewModel>();
services.AddTransient<StationManagementPageViewModel>();
services.AddTransient<ScreenSimulatorPageViewModel>();
services.AddTransient<SettingsPageViewModel>();
services.AddTransient<PluginManagementPageViewModel>();
services.AddTransient<PluginManagementPageViewModel>();
PluginService.InitializePlugins(context, services, parsedOptions.ExternalPluginPath);
})
.Build();
.Build();
InitializeComponent();
}
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
MainWindow = AppHost.Services.GetService<MainWindow>();
MainWindow?.Activate();
AppHost.Services.GetService<StyleManager>();//init
ShowSplashScreen();
}
private void ShowSplashScreen()
{
var splashScreenWindow = AppHost.Services.GetService<StartWindow>();
splashScreenWindow.SplashScreenClosed += OnSplashScreenClosed;
splashScreenWindow.Activate();
_ = PerformInitializationAsync(splashScreenWindow);
}
private static async Task PerformInitializationAsync(StartWindow splashScreen)
{
try
{
var startWindow = AppHost.Services.GetService<StartWindow>();
await Task.Delay(2000);
startWindow.Status = "正在加载设置...";
AppHost.Services.GetService<ISettingsService>().LoadSettings();
await Task.Delay(500);
startWindow.Status = "正在加载数据库...";
AppHost.Services.GetService<IDatabaseService>().ImportData(AppPaths.ConfigFilePath);
await Task.Delay(500);
startWindow.Status = new[] { "正在控票…", "正在关闭垃圾桶盖…", "正在刷绿车底…", "正在准备降弓用刑…" }[new Random().Next(4)];
await Task.Delay(500);
}
catch (Exception ex)
{
Console.WriteLine($"初始化错误: {ex.Message}");
}
finally
{
splashScreen.CompleteInitialization();
}
}
private void OnSplashScreenClosed(object sender, EventArgs e)
{
var mainWindow = AppHost.Services.GetService<MainWindow>();
mainWindow.Activate();
MainWindow = mainWindow;
}
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
@@ -61,7 +102,7 @@
( __|_|_[___[___[___[__]
=-(_)--(_)--' (O) (O) ");
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine(" CRSim - 国铁信息显示模拟");
Console.WriteLine(" CRSim - 国铁信息显示模拟 v" + AppVersion);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Debug console started.\n");
Console.ForegroundColor = ConsoleColor.White;

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 579 KiB

View File

@@ -24,6 +24,7 @@
<ItemGroup>
<None Remove="Styles\Controls.xaml" />
<None Remove="Views\DialogContents\InputDialog.xaml" />
<None Remove="Views\StartWindow.xaml" />
</ItemGroup>
<ItemGroup>
@@ -56,6 +57,7 @@
<PackageReference Include="EPPlus" Version="8.0.8" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.7" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.7" />
<PackageReference Include="Microsoft.UI.Xaml" Version="2.8.7" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.26100.4654" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.7.250606001" />
<PackageReference Include="Microsoft.Xaml.Behaviors.WinUI.Managed" Version="3.0.0" />
@@ -104,6 +106,9 @@
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
<Page Update="Views\StartWindow.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Update="Styles\Controls.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>

View File

@@ -6,9 +6,12 @@ global using System.Text.Json;
global using System.Threading.Tasks;
global using System.Collections.Generic;
global using System.Collections.ObjectModel;
global using System.Reflection;
global using System.Timers;
global using Microsoft.UI.Xaml;
global using Microsoft.UI.Xaml.Controls;
global using Microsoft.UI.Dispatching;
global using Microsoft.UI.Windowing;
global using Microsoft.Extensions.Hosting;
global using Microsoft.Extensions.DependencyInjection;
@@ -26,4 +29,8 @@ global using CRSim.Core.Models;
global using CRSim.Core.Services;
global using CRSim.Core.Abstractions;
global using OfficeOpenXml;
global using OfficeOpenXml;
global using CommunityToolkit.WinUI;
global using Windows.Graphics;

View File

@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<Window
x:Class="CRSim.Views.StartWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:local="using:CRSim.Views"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:converters="using:CRSim.Converters"
xmlns:converters1="using:CommunityToolkit.WinUI.Converters"
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:controls="using:CommunityToolkit.WinUI.Controls"
xmlns:winui="using:CommunityToolkit.WinUI"
mc:Ignorable="d">
<Window.SystemBackdrop>
<MicaBackdrop />
</Window.SystemBackdrop>
<Grid>
<MediaPlayerElement x:Name="Logo" Source="ms-appx:///Assets/Background/CRSim.wmv" AutoPlay="True" />
<ProgressRing IsActive="True" Width="45" Height="45" Foreground="DeepSkyBlue"
IsIndeterminate="True" HorizontalAlignment="Left" Margin="12,0,0,0"/>
<Grid VerticalAlignment="Bottom" Margin="8">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{x:Bind Status,Mode=OneWay}" Foreground="DarkGray"/>
<TextBlock Grid.Column="1" Text="{x:Bind AppVersion}" Foreground="DarkGray"/>
</Grid>
</Grid>
</Window>

View File

@@ -0,0 +1,71 @@
using System.ComponentModel;
namespace CRSim.Views
{
// 定义事件委托类型
public delegate void SplashScreenClosedEventHandler(object sender, EventArgs e);
public sealed partial class StartWindow : Window, INotifyPropertyChanged
{
// 声明事件
public event SplashScreenClosedEventHandler SplashScreenClosed;
public string AppVersion { get; set; } = App.AppVersion;
private string _status = "正在启动...";
public string Status
{
get => _status;
set
{
if (_status != value)
{
_status = value;
OnPropertyChanged(nameof(Status));
}
}
}
public event System.ComponentModel.PropertyChangedEventHandler? PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
public StartWindow()
{
this.InitializeComponent();
AppWindow.Resize(new Windows.Graphics.SizeInt32(750, 270));
var area = DisplayArea.GetFromWindowId(AppWindow.Id, DisplayAreaFallback.Nearest)?.WorkArea;
if (area != null)
{
AppWindow.Move(new PointInt32((area.Value.Width - AppWindow.Size.Width) / 2, (area.Value.Height - AppWindow.Size.Height) / 2));
}
OverlappedPresenter presenter = OverlappedPresenter.Create();
presenter.IsAlwaysOnTop = true;
presenter.IsMaximizable = false;
presenter.IsMinimizable = false;
presenter.IsResizable = false;
presenter.SetBorderAndTitleBar(true, false);
AppWindow.SetPresenter(presenter);
AppWindow.MoveInZOrderAtTop();
Logo.MediaPlayer.Play();
}
// 完成初始化,结束进度条并关闭
public void CompleteInitialization()
{
SplashScreenClosed?.Invoke(this, EventArgs.Empty);
this.Close();
}
}
}