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