mirror of
https://github.com/denglihong2007/CRSim
synced 2026-08-10 11:36:07 +08:00
feat: Implement global exception handling
This commit introduces a global exception handler to prevent the application from crashing due to unhandled exceptions. - Added a `DispatcherUnhandledException` event handler in `App.xaml.cs` to catch all unhandled exceptions on the UI thread. - Created a new `ErrorDialog.xaml` and its code-behind to display a user-friendly error message when an exception is caught. - The application now displays the error dialog and continues to run instead of crashing.
This commit is contained in:
@@ -1,17 +1,16 @@
|
||||
using CRSim.Core.Abstractions;
|
||||
using CRSim.Core.Services;
|
||||
using CRSim.Core.Abstractions;
|
||||
using CRSim.Core.Models;
|
||||
using System.Windows.Threading;
|
||||
|
||||
namespace CRSim.ScreenSimulator.Abstractions
|
||||
{
|
||||
public interface IScreenViewModel
|
||||
{
|
||||
public TimeService TimeService { get; }
|
||||
public Dispatcher UIDispatcher { get; set; }
|
||||
public string Text { get; set; }
|
||||
public int Location { get; set; }
|
||||
public string Video { get; set; }
|
||||
|
||||
public void LoadData(string station, string ticketCheck, string platformName);
|
||||
ITimeService TimeService { get; }
|
||||
Dispatcher UIDispatcher { get; set; }
|
||||
void LoadData(Station station, TicketCheck? ticketCheck, string platform);
|
||||
string? Text { get; set; }
|
||||
Uri Video { get; set; }
|
||||
int Location { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
using CRSim.ScreenSimulator.Views;
|
||||
using System.Windows.Threading;
|
||||
|
||||
namespace CRSim
|
||||
{
|
||||
public partial class App : Application
|
||||
@@ -12,6 +15,7 @@
|
||||
public App()
|
||||
{
|
||||
InitializeComponent();
|
||||
this.DispatcherUnhandledException += App_DispatcherUnhandledException;
|
||||
}
|
||||
|
||||
public static T GetService<T>()
|
||||
@@ -113,5 +117,12 @@
|
||||
Console.WriteLine("Debug console started.\n");
|
||||
Console.ForegroundColor = ConsoleColor.White;
|
||||
}
|
||||
|
||||
private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
|
||||
{
|
||||
var dialog = new ErrorDialog(e.Exception.ToString());
|
||||
dialog.ShowDialog();
|
||||
e.Handled = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user