Browse Source

Merge pull request #25 from ionite34/add-exception-window

pull/5/head
Ionite 2 years ago committed by GitHub
parent
commit
9cd94f1ec8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 24
      StabilityMatrix/App.xaml.cs
  2. 112
      StabilityMatrix/ExceptionWindow.xaml
  3. 17
      StabilityMatrix/ExceptionWindow.xaml.cs
  4. 5
      StabilityMatrix/StabilityMatrix.csproj
  5. 12
      StabilityMatrix/ViewModels/ExceptionWindowViewModel.cs

24
StabilityMatrix/App.xaml.cs

@ -1,4 +1,6 @@
using System; using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO; using System.IO;
using System.Windows; using System.Windows;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
@ -24,6 +26,11 @@ namespace StabilityMatrix
private void App_OnStartup(object sender, StartupEventArgs e) private void App_OnStartup(object sender, StartupEventArgs e)
{ {
// Configure window exception handler when not in debug mode
if (!Debugger.IsAttached)
{
DispatcherUnhandledException += App_DispatcherUnhandledException;
}
var serviceCollection = new ServiceCollection(); var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton<IPageService, PageService>(); serviceCollection.AddSingleton<IPageService, PageService>();
serviceCollection.AddTransient<MainWindow>(); serviceCollection.AddTransient<MainWindow>();
@ -61,5 +68,22 @@ namespace StabilityMatrix
{ {
serviceProvider?.GetRequiredService<LaunchViewModel>().OnShutdown(); serviceProvider?.GetRequiredService<LaunchViewModel>().OnShutdown();
} }
[DoesNotReturn]
private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
var vm = new ExceptionWindowViewModel
{
Exception = e.Exception
};
var exceptionWindow = new ExceptionWindow
{
DataContext = vm,
Owner = MainWindow
};
exceptionWindow.ShowDialog();
e.Handled = true;
Environment.Exit(1);
}
} }
} }

112
StabilityMatrix/ExceptionWindow.xaml

@ -0,0 +1,112 @@
<ui:FluentWindow
Background="{DynamicResource ApplicationBackgroundBrush}"
ExtendsContentIntoTitleBar="True"
Foreground="{DynamicResource TextFillColorPrimaryBrush}"
Height="500"
Loaded="ExceptionWindow_OnLoaded"
ResizeMode="NoResize"
Title="Unexpected Application Error"
Width="800"
WindowStartupLocation="CenterOwner"
mc:Ignorable="d"
x:Class="StabilityMatrix.ExceptionWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
xmlns:local="clr-namespace:StabilityMatrix"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
xmlns:viewModels="clr-namespace:StabilityMatrix.ViewModels"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Window.DataContext>
<viewModels:ExceptionWindowViewModel />
</Window.DataContext>
<Window.Resources>
<Style TargetType="TextBlock" x:Key="Label">
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="Margin" Value="0,8,0,4" />
</Style>
<Style TargetType="TextBlock" x:Key="Content">
<Setter Property="Margin" Value="0,4,0,8" />
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="*" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<ui:TitleBar Background="DarkOrange">
<ui:TitleBar.Header>
<TextBlock Margin="16,8" Text="Unexpected error" />
</ui:TitleBar.Header>
</ui:TitleBar>
<Grid Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="*" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<!-- Info -->
<TextBlock
FontSize="14"
Grid.Row="0"
Margin="16"
Text="An unexpected error occured."
TextWrapping="Wrap"
VerticalAlignment="Top" />
<!-- Exception details -->
<Border Grid.Row="1" VerticalAlignment="Stretch">
<Expander
Header="Exception Details"
IsExpanded="True"
Margin="16,8,16,8"
VerticalAlignment="Top">
<ScrollViewer
CanContentScroll="True"
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto">
<StackPanel>
<!-- Exception Message -->
<TextBlock Style="{StaticResource Label}" Text="Exception message" />
<TextBlock
Grid.Column="1"
Text="{Binding Exception.Message, Mode=OneWay, TargetNullValue=-}"
TextWrapping="Wrap" />
<!-- Exception Type -->
<TextBlock Style="{StaticResource Label}" Text="Exception type" />
<TextBlock Style="{StaticResource Content}" Text="{Binding ExceptionType, Mode=OneWay, TargetNullValue=-}" />
<!-- Callstack -->
<TextBlock Style="{StaticResource Label}" Text="Callstack" />
<TextBlock Style="{StaticResource Content}" Text="{Binding Exception.StackTrace, Mode=OneWay, TargetNullValue=-}" />
<!-- Inner exception -->
<TextBlock Style="{StaticResource Label}" Text="Inner exception" />
<TextBlock Style="{StaticResource Content}" Text="{Binding Exception.InnerException, Mode=OneWay, TargetNullValue=-}" />
</StackPanel>
</ScrollViewer>
</Expander>
</Border>
<!-- Close Button -->
<Button
Content="Exit Application"
Grid.Row="2"
HorizontalAlignment="Right"
Margin="16"
Padding="8">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<i:CallMethodAction MethodName="Close" TargetObject="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</Grid>
</Grid>
</ui:FluentWindow>

17
StabilityMatrix/ExceptionWindow.xaml.cs

@ -0,0 +1,17 @@
using System.Windows;
using Wpf.Ui.Controls.Window;
namespace StabilityMatrix;
public partial class ExceptionWindow : FluentWindow
{
public ExceptionWindow()
{
InitializeComponent();
}
private void ExceptionWindow_OnLoaded(object sender, RoutedEventArgs e)
{
System.Media.SystemSounds.Hand.Play();
}
}

5
StabilityMatrix/StabilityMatrix.csproj

@ -10,6 +10,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.0" /> <PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.1" /> <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.1" />
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.39" />
<PackageReference Include="NLog" Version="5.1.4" /> <PackageReference Include="NLog" Version="5.1.4" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.2.3" /> <PackageReference Include="NLog.Extensions.Logging" Version="5.2.3" />
<PackageReference Include="Ookii.Dialogs.Wpf" Version="5.0.1" /> <PackageReference Include="Ookii.Dialogs.Wpf" Version="5.0.1" />
@ -31,6 +32,10 @@
<Content Include="Assets\Icon.ico"> <Content Include="Assets\Icon.ico">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content> </Content>
<None Remove="Assets\Git-2.40.1-64-bit.exe" />
<Content Include="Assets\Git-2.40.1-64-bit.exe">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

12
StabilityMatrix/ViewModels/ExceptionWindowViewModel.cs

@ -0,0 +1,12 @@
using System;
using System.Windows;
using CommunityToolkit.Mvvm.Input;
namespace StabilityMatrix.ViewModels;
public class ExceptionWindowViewModel
{
public Exception Exception { get; set; }
public string ExceptionType => Exception.GetType().Name;
}
Loading…
Cancel
Save