OSDN Git Service

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