Merge pull request #310 from denglihong2007/dev

chore: 优化代码质量 [skip cliff]
This commit is contained in:
denglihong2007
2026-01-31 22:11:35 +08:00
committed by GitHub
8 changed files with 55 additions and 24 deletions

View File

@@ -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(@"
___ _____________________

View File

@@ -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; }
}
}

14
CRSim/NativeMethods.cs Normal file
View File

@@ -0,0 +1,14 @@
using System.Runtime.InteropServices;
namespace CRSim
{
public partial class NativeMethods
{
/// <summary>
/// 为调用进程分配一个新的控制台。
/// </summary>
/// <returns>如果函数成功,返回值为非零;如果失败,返回值为零。</returns>
[LibraryImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static partial bool AllocConsole();
}
}

View File

@@ -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<TrainStop?> 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<TrainStop?> EditTrainNumberStopAsync(TrainStop trainStop)
{
bool isButtonEnabled = false;
ContentDialog dialog = new()
{
XamlRoot = XamlRoot,
@@ -152,7 +155,6 @@ namespace CRSim.Services
public async Task<TrainStop?> GetInputTrainStopAsync(List<WaitingArea> waitingAreas, List<string> platforms)
{
bool isButtonEnabled = false;
ContentDialog dialog = new()
{
XamlRoot = XamlRoot,
@@ -180,7 +182,6 @@ namespace CRSim.Services
public async Task<TrainStop?> EditInputTrainStopAsync(List<WaitingArea> waitingAreas, List<string> 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;
}

View File

@@ -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} 可用,请前往“设置”下载安装更新!";
}

View File

@@ -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);

View File

@@ -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));
}

View File

@@ -33,7 +33,8 @@ public partial class StationManagementPageViewModel : ObservableObject
public ObservableCollection<Platform> Platforms { get; private set; } = [];
public ObservableCollection<TrainStop> TrainStops { get; private set; } = [];
public ObservableCollection<TrainStop> FilteredTrainStops =>[.. TrainStops.Where(x => x.Number.ToUpper().Contains(SearchTrainNumberText.ToUpper()))];
public ObservableCollection<TrainStop> FilteredTrainStops =>
[.. TrainStops.Where(x => x.Number.Contains(SearchTrainNumberText, StringComparison.OrdinalIgnoreCase))];
private readonly IDatabaseService _databaseService = App.GetService<IDatabaseService>();
@@ -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;