Skip to content

Commit 9d2fff2

Browse files
committed
* Now Visual Styles for Windows Forms will be enabled for WPF application to show the Crash Report form in native Windows Visual Style.
* Added WPF example.
1 parent f4ae128 commit 9d2fff2

13 files changed

Lines changed: 581 additions & 0 deletions

CrashReporter.NET.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CrashReporter.NET", "CrashR
77
EndProject
88
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CrashReporterTest", "CrashReporterTest\CrashReporterTest.csproj", "{895AE503-6920-4BFF-9D47-3A91F90E8606}"
99
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CrashReporterWPFTest", "CrashReporterWPFTest\CrashReporterWPFTest.csproj", "{0DC93E03-047D-4664-BE24-2215BF98C8A1}"
11+
EndProject
1012
Global
1113
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1214
Debug|Any CPU = Debug|Any CPU
@@ -21,6 +23,10 @@ Global
2123
{895AE503-6920-4BFF-9D47-3A91F90E8606}.Debug|Any CPU.Build.0 = Debug|Any CPU
2224
{895AE503-6920-4BFF-9D47-3A91F90E8606}.Release|Any CPU.ActiveCfg = Release|Any CPU
2325
{895AE503-6920-4BFF-9D47-3A91F90E8606}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{0DC93E03-047D-4664-BE24-2215BF98C8A1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{0DC93E03-047D-4664-BE24-2215BF98C8A1}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{0DC93E03-047D-4664-BE24-2215BF98C8A1}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{0DC93E03-047D-4664-BE24-2215BF98C8A1}.Release|Any CPU.Build.0 = Release|Any CPU
2430
EndGlobalSection
2531
GlobalSection(SolutionProperties) = preSolution
2632
HideSolutionNode = FALSE

CrashReporter.NET/ReportCrash.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.IO;
66
using System.Reflection;
77
using System.Threading;
8+
using System.Windows.Forms;
89

910
namespace CrashReporterDotNET
1011
{
@@ -138,6 +139,10 @@ public void Send(Exception exception)
138139
if (String.IsNullOrEmpty(ToEmail) || !AnalyzeWithDoctorDump && (String.IsNullOrEmpty(FromEmail) || String.IsNullOrEmpty(SmtpHost)))
139140
return;
140141

142+
if (!Application.MessageLoop)
143+
{
144+
Application.EnableVisualStyles();
145+
}
141146
var parameterizedThreadStart = new ParameterizedThreadStart(delegate
142147
{
143148
new CrashReport(this).ShowDialog();

CrashReporterWPFTest/App.xaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Application x:Class="CrashReporterWPFTest.App"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:local="clr-namespace:CrashReporterWPFTest"
5+
StartupUri="MainWindow.xaml">
6+
<Application.Resources>
7+
8+
</Application.Resources>
9+
</Application>

CrashReporterWPFTest/App.xaml.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using System.Windows;
4+
using System.Windows.Threading;
5+
using CrashReporterDotNET;
6+
7+
namespace CrashReporterWPFTest
8+
{
9+
/// <summary>
10+
/// Interaction logic for App.xaml
11+
/// </summary>
12+
public partial class App : Application
13+
{
14+
protected override void OnStartup(StartupEventArgs e)
15+
{
16+
base.OnStartup(e);
17+
AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
18+
Application.Current.DispatcherUnhandledException += DispatcherOnUnhandledException;
19+
TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException;
20+
}
21+
22+
private void TaskSchedulerOnUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs unobservedTaskExceptionEventArgs)
23+
{
24+
ReportCrash(unobservedTaskExceptionEventArgs.Exception);
25+
Environment.Exit(0);
26+
}
27+
28+
private void DispatcherOnUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs dispatcherUnhandledExceptionEventArgs)
29+
{
30+
ReportCrash(dispatcherUnhandledExceptionEventArgs.Exception);
31+
Environment.Exit(0);
32+
}
33+
34+
private static void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs unhandledExceptionEventArgs)
35+
{
36+
ReportCrash((Exception)unhandledExceptionEventArgs.ExceptionObject);
37+
Environment.Exit(0);
38+
}
39+
40+
public static void ReportCrash(Exception exception, string developerMessage = "")
41+
{
42+
var reportCrash = new ReportCrash
43+
{
44+
DeveloperMessage = developerMessage,
45+
ToEmail = "Email where you want to receive crash reports."
46+
};
47+
reportCrash.Send(exception);
48+
}
49+
}
50+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{0DC93E03-047D-4664-BE24-2215BF98C8A1}</ProjectGuid>
8+
<OutputType>WinExe</OutputType>
9+
<RootNamespace>CrashReporterWPFTest</RootNamespace>
10+
<AssemblyName>CrashReporterWPFTest</AssemblyName>
11+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
14+
<WarningLevel>4</WarningLevel>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<PlatformTarget>AnyCPU</PlatformTarget>
18+
<DebugSymbols>true</DebugSymbols>
19+
<DebugType>full</DebugType>
20+
<Optimize>false</Optimize>
21+
<OutputPath>bin\Debug\</OutputPath>
22+
<DefineConstants>DEBUG;TRACE</DefineConstants>
23+
<ErrorReport>prompt</ErrorReport>
24+
<WarningLevel>4</WarningLevel>
25+
</PropertyGroup>
26+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
27+
<PlatformTarget>AnyCPU</PlatformTarget>
28+
<DebugType>pdbonly</DebugType>
29+
<Optimize>true</Optimize>
30+
<OutputPath>bin\Release\</OutputPath>
31+
<DefineConstants>TRACE</DefineConstants>
32+
<ErrorReport>prompt</ErrorReport>
33+
<WarningLevel>4</WarningLevel>
34+
</PropertyGroup>
35+
<ItemGroup>
36+
<Reference Include="System" />
37+
<Reference Include="System.Data" />
38+
<Reference Include="System.Xml" />
39+
<Reference Include="Microsoft.CSharp" />
40+
<Reference Include="System.Core" />
41+
<Reference Include="System.Xml.Linq" />
42+
<Reference Include="System.Data.DataSetExtensions" />
43+
<Reference Include="System.Xaml">
44+
<RequiredTargetFramework>4.0</RequiredTargetFramework>
45+
</Reference>
46+
<Reference Include="WindowsBase" />
47+
<Reference Include="PresentationCore" />
48+
<Reference Include="PresentationFramework" />
49+
</ItemGroup>
50+
<ItemGroup>
51+
<ApplicationDefinition Include="App.xaml">
52+
<Generator>MSBuild:Compile</Generator>
53+
<SubType>Designer</SubType>
54+
</ApplicationDefinition>
55+
<Page Include="MainWindow.xaml">
56+
<Generator>MSBuild:Compile</Generator>
57+
<SubType>Designer</SubType>
58+
</Page>
59+
<Compile Include="App.xaml.cs">
60+
<DependentUpon>App.xaml</DependentUpon>
61+
<SubType>Code</SubType>
62+
</Compile>
63+
<Compile Include="MainWindow.xaml.cs">
64+
<DependentUpon>MainWindow.xaml</DependentUpon>
65+
<SubType>Code</SubType>
66+
</Compile>
67+
</ItemGroup>
68+
<ItemGroup>
69+
<Compile Include="Properties\AssemblyInfo.cs">
70+
<SubType>Code</SubType>
71+
</Compile>
72+
<Compile Include="Properties\Resources.Designer.cs">
73+
<AutoGen>True</AutoGen>
74+
<DesignTime>True</DesignTime>
75+
<DependentUpon>Resources.resx</DependentUpon>
76+
</Compile>
77+
<Compile Include="Properties\Settings.Designer.cs">
78+
<AutoGen>True</AutoGen>
79+
<DependentUpon>Settings.settings</DependentUpon>
80+
<DesignTimeSharedInput>True</DesignTimeSharedInput>
81+
</Compile>
82+
<EmbeddedResource Include="Properties\Resources.resx">
83+
<Generator>ResXFileCodeGenerator</Generator>
84+
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
85+
</EmbeddedResource>
86+
<None Include="Properties\Settings.settings">
87+
<Generator>SettingsSingleFileGenerator</Generator>
88+
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
89+
</None>
90+
</ItemGroup>
91+
<ItemGroup>
92+
<ProjectReference Include="..\CrashReporter.NET\CrashReporter.NET.csproj">
93+
<Project>{67912bda-1572-46b0-8623-5872d9dadbc8}</Project>
94+
<Name>CrashReporter.NET</Name>
95+
</ProjectReference>
96+
</ItemGroup>
97+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
98+
</Project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Window x:Class="CrashReporterWPFTest.MainWindow"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
xmlns:local="clr-namespace:CrashReporterWPFTest"
7+
mc:Ignorable="d"
8+
Title="MainWindow" Height="93" Width="150" ResizeMode="NoResize">
9+
<Grid>
10+
<Grid.ColumnDefinitions>
11+
<ColumnDefinition Width="36*"/>
12+
<ColumnDefinition Width="87*"/>
13+
<ColumnDefinition Width="19*"/>
14+
</Grid.ColumnDefinitions>
15+
<Button x:Name="buttonCrash" Content="Crash this app" Margin="10,10,10,0" VerticalAlignment="Top" Grid.ColumnSpan="3" Height="41" Click="buttonCrash_Click"/>
16+
17+
</Grid>
18+
</Window>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
using System.IO;
3+
using System.Threading;
4+
using System.Windows;
5+
6+
namespace CrashReporterWPFTest
7+
{
8+
/// <summary>
9+
/// Interaction logic for MainWindow.xaml
10+
/// </summary>
11+
public partial class MainWindow : Window
12+
{
13+
public MainWindow()
14+
{
15+
InitializeComponent();
16+
}
17+
18+
private void buttonCrash_Click(object sender, RoutedEventArgs e)
19+
{
20+
var thread = new Thread(ThrowException);
21+
thread.Start();
22+
}
23+
24+
private void ThrowException()
25+
{
26+
try
27+
{
28+
throw new ArgumentException();
29+
}
30+
catch (ArgumentException argumentException)
31+
{
32+
const string path = "test.txt";
33+
try
34+
{
35+
if (!File.Exists(path))
36+
{
37+
throw new FileNotFoundException(
38+
"File Not found when trying to write argument exception to the file", argumentException);
39+
}
40+
}
41+
catch (Exception exception)
42+
{
43+
App.ReportCrash(exception, "Value of path variable is " + path);
44+
}
45+
}
46+
}
47+
}
48+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System.Reflection;
2+
using System.Resources;
3+
using System.Runtime.CompilerServices;
4+
using System.Runtime.InteropServices;
5+
using System.Windows;
6+
7+
// General Information about an assembly is controlled through the following
8+
// set of attributes. Change these attribute values to modify the information
9+
// associated with an assembly.
10+
[assembly: AssemblyTitle("CrashReporterWPFTest")]
11+
[assembly: AssemblyDescription("")]
12+
[assembly: AssemblyConfiguration("")]
13+
[assembly: AssemblyCompany("")]
14+
[assembly: AssemblyProduct("CrashReporterWPFTest")]
15+
[assembly: AssemblyCopyright("Copyright © 2017")]
16+
[assembly: AssemblyTrademark("")]
17+
[assembly: AssemblyCulture("")]
18+
19+
// Setting ComVisible to false makes the types in this assembly not visible
20+
// to COM components. If you need to access a type in this assembly from
21+
// COM, set the ComVisible attribute to true on that type.
22+
[assembly: ComVisible(false)]
23+
24+
//In order to begin building localizable applications, set
25+
//<UICulture>CultureYouAreCodingWith</UICulture> in your .csproj file
26+
//inside a <PropertyGroup>. For example, if you are using US english
27+
//in your source files, set the <UICulture> to en-US. Then uncomment
28+
//the NeutralResourceLanguage attribute below. Update the "en-US" in
29+
//the line below to match the UICulture setting in the project file.
30+
31+
//[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]
32+
33+
34+
[assembly: ThemeInfo(
35+
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
36+
//(used if a resource is not found in the page,
37+
// or application resource dictionaries)
38+
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
39+
//(used if a resource is not found in the page,
40+
// app, or any theme specific resource dictionaries)
41+
)]
42+
43+
44+
// Version information for an assembly consists of the following four values:
45+
//
46+
// Major Version
47+
// Minor Version
48+
// Build Number
49+
// Revision
50+
//
51+
// You can specify all the values or you can default the Build and Revision Numbers
52+
// by using the '*' as shown below:
53+
// [assembly: AssemblyVersion("1.0.*")]
54+
[assembly: AssemblyVersion("1.0.0.0")]
55+
[assembly: AssemblyFileVersion("1.0.0.0")]

CrashReporterWPFTest/Properties/Resources.Designer.cs

Lines changed: 71 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)