Browse Source

[base] 添加框架异常处理

master
fajiao 1 year ago
parent
commit
d66eb926d7
  1. 42
      JiLinApp/Core/Avalonia/RxAppExceptionHandler.cs
  2. 1
      JiLinApp/JiLinApp.csproj
  3. 42
      JiLinApp/Program.cs

42
JiLinApp/Core/Avalonia/RxAppExceptionHandler.cs

@ -0,0 +1,42 @@
using System;
using System.Diagnostics;
namespace JiLinApp.Core.Avalonia;
public class RxAppExceptionHandler : IObserver<Exception>
{
private static RxAppExceptionHandler? _instance;
public static RxAppExceptionHandler Instance
{
get
{
return _instance ??= new();
}
}
public delegate void ExceptionHandler(Exception e);
public event ExceptionHandler? OnExceptionHandler;
public void OnNext(Exception e)
{
if (Debugger.IsAttached) Debugger.Break();
OnException(e);
}
public void OnError(Exception e)
{
if (Debugger.IsAttached) Debugger.Break();
OnException(e);
}
public void OnCompleted()
{
}
private void OnException(Exception e)
{
OnExceptionHandler?.Invoke(e);
}
}

1
JiLinApp/JiLinApp.csproj

@ -6,6 +6,7 @@
<BuiltInComInteropSupport>true</BuiltInComInteropSupport> <BuiltInComInteropSupport>true</BuiltInComInteropSupport>
<ApplicationManifest>app.manifest</ApplicationManifest> <ApplicationManifest>app.manifest</ApplicationManifest>
<AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault> <AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>
<SatelliteResourceLanguages>zh-Hans</SatelliteResourceLanguages>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

42
JiLinApp/Program.cs

@ -1,7 +1,10 @@
using Avalonia; using Avalonia;
using Avalonia.Media; using Avalonia.Media;
using Avalonia.ReactiveUI; using Avalonia.ReactiveUI;
using JiLinApp.Core.Avalonia;
using ReactiveUI;
using System; using System;
using System.Threading.Tasks;
namespace JiLinApp; namespace JiLinApp;
@ -11,12 +14,25 @@ internal class Program
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized // SynchronizationContext-reliant code before AppMain is called: things aren't initialized
// yet and stuff might break. // yet and stuff might break.
[STAThread] [STAThread]
public static void Main(string[] args) => BuildAvaloniaApp() public static void Main(string[] args)
.StartWithClassicDesktopLifetime(args); {
GlobalInit();
try
{
AppBuilder appBuilder = BuildAvaloniaApp();
appBuilder.StartWithClassicDesktopLifetime(args);
}
catch (Exception)
{
//The global try-catch
throw;
}
}
// Avalonia configuration, don't remove; also used by visual designer. // Avalonia configuration, don't remove; also used by visual designer.
public static AppBuilder BuildAvaloniaApp() public static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>() {
return AppBuilder.Configure<App>()
.UsePlatformDetect() .UsePlatformDetect()
.WithInterFont() .WithInterFont()
.LogToTrace() .LogToTrace()
@ -32,4 +48,24 @@ internal class Program
} }
} }
}); });
}
#region extend
private static void GlobalInit()
{
// HandleExceptions
// Exceptions from another thread
TaskScheduler.UnobservedTaskException += (sender, e) =>
{
e.SetObserved();
};
// Exceptions from Reactive UI
RxAppExceptionHandler.Instance.OnExceptionHandler += e =>
{
};
RxApp.DefaultExceptionHandler = RxAppExceptionHandler.Instance;
}
#endregion extend
} }
Loading…
Cancel
Save