annotate SilverlightGlimpse/SilverlightGlimpse.Test/App.xaml.cs @ 62:810116cd6b8e

ErrorWindow working
author Steven Hollidge <stevenhollidge@hotmail.com>
date Sun, 22 Apr 2012 09:01:20 +0100
parents
children 536498832a79
rev   line source
62
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
1 using System;
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
2 using System.Collections.Generic;
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
3 using System.Linq;
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
4 using System.Net;
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
5 using System.Windows;
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
6 using System.Windows.Controls;
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
7 using System.Windows.Documents;
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
8 using System.Windows.Input;
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
9 using System.Windows.Media;
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
10 using System.Windows.Media.Animation;
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
11 using System.Windows.Shapes;
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
12 using SilverlightGlimpse.Services;
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
13 //using Glimpse;
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
14
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
15 namespace SilverlightGlimpse.Test
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
16 {
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
17 public partial class App : Application
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
18 {
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
19
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
20 public App()
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
21 {
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
22 this.Startup += this.Application_Startup;
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
23 this.Exit += this.Application_Exit;
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
24 this.UnhandledException += this.Application_UnhandledException;
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
25
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
26 InitializeComponent();
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
27 }
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
28
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
29 private void Application_Startup(object sender, StartupEventArgs e)
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
30 {
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
31 string appName = "SilverlightGlimpse";
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
32
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
33 try
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
34 {
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
35 this.RootVisual = new MainPage();
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
36 GlimpseService.CreateInstance.Load(this, appName);
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
37 }
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
38 catch (Exception ex)
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
39 {
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
40 GlimpseService.CreateInstance.DisplayLoadFailure(this, ex);
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
41 }
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
42 }
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
43
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
44 private void Application_Exit(object sender, EventArgs e)
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
45 {
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
46
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
47 }
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
48
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
49 private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
50 {
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
51 // If the app is running outside of the debugger then report the exception using
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
52 // the browser's exception mechanism. On IE this will display it a yellow alert
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
53 // icon in the status bar and Firefox will display a script error.
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
54 if (!System.Diagnostics.Debugger.IsAttached)
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
55 {
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
56
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
57 // NOTE: This will allow the application to continue running after an exception has been thrown
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
58 // but not handled.
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
59 // For production applications this error handling should be replaced with something that will
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
60 // report the error to the website and stop the application.
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
61 e.Handled = true;
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
62 Deployment.Current.Dispatcher.BeginInvoke(delegate { ReportErrorToDOM(e); });
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
63 }
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
64 }
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
65
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
66 private void ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e)
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
67 {
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
68 try
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
69 {
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
70 string errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace;
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
71 errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n");
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
72
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
73 System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(\"Unhandled Error in Silverlight Application " + errorMsg + "\");");
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
74 }
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
75 catch (Exception)
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
76 {
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
77 }
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
78 }
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
79 }
810116cd6b8e ErrorWindow working
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff changeset
80 }