From 9557db5484a25c9796f9af950206a59bee9a0454 Mon Sep 17 00:00:00 2001 From: denglihong2007 <98096191+denglihong2007@users.noreply.github.com> Date: Sat, 31 Jan 2026 22:10:27 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E8=B4=A8=E9=87=8F=20[skip=20cliff]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CRSim/App.xaml.cs | 12 +++---- CRSim/Models/InfoItem.cs | 6 ++-- CRSim/NativeMethods.cs | 14 ++++++++ CRSim/Services/DialogService.cs | 35 +++++++++++++------ CRSim/ViewModels/DashboardPageViewModel.cs | 2 +- .../PlatformDiagramPageViewModel.cs | 1 + .../PluginManagementPageViewModel.cs | 2 ++ .../StationManagementPageViewModel.cs | 7 ++-- 8 files changed, 55 insertions(+), 24 deletions(-) create mode 100644 CRSim/NativeMethods.cs diff --git a/CRSim/App.xaml.cs b/CRSim/App.xaml.cs index 6b18293..054f06e 100644 --- a/CRSim/App.xaml.cs +++ b/CRSim/App.xaml.cs @@ -4,7 +4,7 @@ { public IHost AppHost { get; private set; } - public static MainWindow MainWindow; + public static MainWindow MainWindow { get; set; } public static string AppVersion { get; set; } private Mutex mutex; @@ -28,7 +28,10 @@ protected override async void OnLaunched(LaunchActivatedEventArgs args) { - AppVersion = Assembly.GetAssembly(typeof(App)).GetName().Version.ToString(); + AppVersion = Assembly.GetAssembly(typeof(App)) + ?.GetName() + ?.Version + ?.ToString() ?? "未知"; mutex = new Mutex(true, "CRSim", out bool isNewInstance); if (!isNewInstance) { @@ -96,12 +99,9 @@ } } - [System.Runtime.InteropServices.DllImport("kernel32.dll")] - private static extern bool AllocConsole(); - public static void LaunchDebugConsole() { - AllocConsole(); + NativeMethods.AllocConsole(); Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine(@" ___ _____________________ diff --git a/CRSim/Models/InfoItem.cs b/CRSim/Models/InfoItem.cs index 2512c5d..ac2ca19 100644 --- a/CRSim/Models/InfoItem.cs +++ b/CRSim/Models/InfoItem.cs @@ -2,8 +2,8 @@ { public class InfoItem { - public string IconGlyph { get; set; } - public string Title { get; set; } - public string Detail { get; set; } + public required string IconGlyph { get; set; } + public required string Title { get; set; } + public required string Detail { get; set; } } } diff --git a/CRSim/NativeMethods.cs b/CRSim/NativeMethods.cs new file mode 100644 index 0000000..7477c8f --- /dev/null +++ b/CRSim/NativeMethods.cs @@ -0,0 +1,14 @@ +using System.Runtime.InteropServices; +namespace CRSim +{ + public partial class NativeMethods + { + /// + /// 为调用进程分配一个新的控制台。 + /// + /// 如果函数成功,返回值为非零;如果失败,返回值为零。 + [LibraryImport("kernel32.dll", SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + public static partial bool AllocConsole(); + } +} diff --git a/CRSim/Services/DialogService.cs b/CRSim/Services/DialogService.cs index 19b5ec9..958c876 100644 --- a/CRSim/Services/DialogService.cs +++ b/CRSim/Services/DialogService.cs @@ -75,8 +75,10 @@ namespace CRSim.Services public string? GetFile(string[] extensions) { - var dlg = new OpenFileDialog(); - dlg.Multiselect = false; + var dlg = new OpenFileDialog + { + Multiselect = false + }; var filters = extensions .Select(ext => @@ -97,7 +99,6 @@ namespace CRSim.Services public async Task GetInputTrainNumberStopAsync() { - bool isButtonEnabled = false; ContentDialog dialog = new() { XamlRoot = XamlRoot, @@ -118,13 +119,15 @@ namespace CRSim.Services if (result == ContentDialogResult.Primary) { var inputDialog = dialog.Content as TrainNumberStopDialog; - return inputDialog.GeneratedTrainStop; + if (inputDialog is not null) + { + return inputDialog.GeneratedTrainStop; + } } return null; } public async Task EditTrainNumberStopAsync(TrainStop trainStop) { - bool isButtonEnabled = false; ContentDialog dialog = new() { XamlRoot = XamlRoot, @@ -152,7 +155,6 @@ namespace CRSim.Services public async Task GetInputTrainStopAsync(List waitingAreas, List platforms) { - bool isButtonEnabled = false; ContentDialog dialog = new() { XamlRoot = XamlRoot, @@ -180,7 +182,6 @@ namespace CRSim.Services public async Task EditInputTrainStopAsync(List waitingAreas, List platforms, TrainStop trainStop) { - bool isButtonEnabled = false; ContentDialog dialog = new() { XamlRoot = XamlRoot, @@ -201,7 +202,10 @@ namespace CRSim.Services if (result == ContentDialogResult.Primary) { var inputDialog = dialog.Content as TrainStopDialog; - return inputDialog.GeneratedTrainStop; + if (inputDialog is not null) + { + return inputDialog.GeneratedTrainStop; + } } return null; } @@ -246,7 +250,10 @@ namespace CRSim.Services if (result == ContentDialogResult.Primary) { var inputDialog = dialog.Content as PlatformDialog; - return inputDialog.GeneratedPlatforms; + if (inputDialog is not null) + { + return inputDialog.GeneratedPlatforms; + } } return null; } @@ -269,7 +276,10 @@ namespace CRSim.Services if (result == ContentDialogResult.Primary) { var inputDialog = dialog.Content as CreateStationDialog; - return inputDialog.GeneratedStation; + if (inputDialog is not null) + { + return inputDialog.GeneratedStation; + } } return null; } @@ -292,7 +302,10 @@ namespace CRSim.Services if (result == ContentDialogResult.Primary) { var inputDialog = dialog.Content as TrainColorsDialog; - return [.. inputDialog.TrainColors]; + if (inputDialog is not null) + { + return [.. inputDialog.TrainColors]; + } } return null; } diff --git a/CRSim/ViewModels/DashboardPageViewModel.cs b/CRSim/ViewModels/DashboardPageViewModel.cs index bb46e97..06058fb 100644 --- a/CRSim/ViewModels/DashboardPageViewModel.cs +++ b/CRSim/ViewModels/DashboardPageViewModel.cs @@ -16,7 +16,7 @@ public partial class DashboardPageViewModel : ObservableObject private async void InitializeAsync() { var updateInfo = await _networkService.GetUpdateAsync(_settingsService.GetSettings().Api.UpdateApi); - if (updateInfo is not null && updateInfo.Name != Assembly.GetExecutingAssembly().GetName().Version.ToString()) + if (updateInfo is not null && updateInfo.Name != App.AppVersion.ToString()) { UpdateMessage = $"有新版本 {updateInfo.Name} 可用,请前往“设置”下载安装更新!"; } diff --git a/CRSim/ViewModels/PlatformDiagramPageViewModel.cs b/CRSim/ViewModels/PlatformDiagramPageViewModel.cs index caa0d5f..99867f8 100644 --- a/CRSim/ViewModels/PlatformDiagramPageViewModel.cs +++ b/CRSim/ViewModels/PlatformDiagramPageViewModel.cs @@ -31,6 +31,7 @@ public partial class PlatformDiagramPageViewModel(IDialogService _dialogService, [RelayCommand] public async Task Generate() { + if(SelectedStation is null) return; if(!CheckStation(SelectedStation,out string detail)) { await _dialogService.ShowTextAsync("警告", detail); diff --git a/CRSim/ViewModels/PluginManagementPageViewModel.cs b/CRSim/ViewModels/PluginManagementPageViewModel.cs index 474a990..38da74e 100644 --- a/CRSim/ViewModels/PluginManagementPageViewModel.cs +++ b/CRSim/ViewModels/PluginManagementPageViewModel.cs @@ -71,6 +71,7 @@ public partial class PluginManagementPageViewModel : ObservableObject [RelayCommand] public void UninstallPlugin() { + if (SelectedPlugin == null) return; SelectedPlugin.IsUninstalling = true; OnPropertyChanged(nameof(SelectedPlugin)); } @@ -78,6 +79,7 @@ public partial class PluginManagementPageViewModel : ObservableObject [RelayCommand] public void CancelUninstallPlugin() { + if (SelectedPlugin == null) return; SelectedPlugin.IsUninstalling = false; OnPropertyChanged(nameof(SelectedPlugin)); } diff --git a/CRSim/ViewModels/StationManagementPageViewModel.cs b/CRSim/ViewModels/StationManagementPageViewModel.cs index 826950b..816aa4b 100644 --- a/CRSim/ViewModels/StationManagementPageViewModel.cs +++ b/CRSim/ViewModels/StationManagementPageViewModel.cs @@ -33,7 +33,8 @@ public partial class StationManagementPageViewModel : ObservableObject public ObservableCollection Platforms { get; private set; } = []; public ObservableCollection TrainStops { get; private set; } = []; - public ObservableCollection FilteredTrainStops =>[.. TrainStops.Where(x => x.Number.ToUpper().Contains(SearchTrainNumberText.ToUpper()))]; + public ObservableCollection FilteredTrainStops => + [.. TrainStops.Where(x => x.Number.Contains(SearchTrainNumberText, StringComparison.OrdinalIgnoreCase))]; private readonly IDatabaseService _databaseService = App.GetService(); @@ -597,7 +598,7 @@ public partial class StationManagementPageViewModel : ObservableObject { "停运" => null, "正点" => TimeSpan.Zero, - var t when t.StartsWith("-") && TimeSpan.TryParseExact(t.Substring(1), @"hh\:mm", null, out var ts) + var t when t.StartsWith('-') && TimeSpan.TryParseExact(t[1..], @"hh\:mm", null, out var ts) => -ts, var t when TimeSpan.TryParseExact(t, @"hh\:mm", null, out var ts) => ts, @@ -636,7 +637,7 @@ public partial class StationManagementPageViewModel : ObservableObject worksheet.Cells[i + 2, 2].Value = TrainStops[i].Length; worksheet.Cells[i + 2, 3].Value = TrainStops[i].ArrivalTime?.ToString(@"hh\:mm") ?? string.Empty; worksheet.Cells[i + 2, 4].Value = TrainStops[i].DepartureTime?.ToString(@"hh\:mm") ?? string.Empty; - worksheet.Cells[i + 2, 5].Value = (string)converter.Convert(TrainStops[i].TicketCheckIds, null, null, null); + worksheet.Cells[i + 2, 5].Value = (string)converter.Convert(TrainStops[i].TicketCheckIds, null, null, string.Empty); worksheet.Cells[i + 2, 6].Value = TrainStops[i].Platform; worksheet.Cells[i + 2, 7].Value = TrainStops[i].Landmark; worksheet.Cells[i + 2, 8].Value = TrainStops[i].Origin;