PageRenderTime 21ms CodeModel.GetById 14ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/V4/Quickstarts/EventAggregation/Silverlight/App.xaml.cs

#
C# | 74 lines | 45 code | 6 blank | 23 comment | 1 complexity | e2be54a00060c21487010907a0ea5d44 MD5 | raw file
 1//===================================================================================
 2// Microsoft patterns & practices
 3// Composite Application Guidance for Windows Presentation Foundation and Silverlight
 4//===================================================================================
 5// Copyright (c) Microsoft Corporation.  All rights reserved.
 6// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
 7// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
 8// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 9// FITNESS FOR A PARTICULAR PURPOSE.
10//===================================================================================
11// The example companies, organizations, products, domain names,
12// e-mail addresses, logos, people, places, and events depicted
13// herein are fictitious.  No association with any real company,
14// organization, product, domain name, email address, logo, person,
15// places, or events is intended or should be inferred.
16//===================================================================================
17using System;
18using System.Windows;
19
20namespace EventAggregation
21{
22    public partial class App : Application
23    {
24        public App()
25        {
26            this.Startup += this.Application_Startup;
27            this.Exit += this.Application_Exit;
28            this.UnhandledException += this.Application_UnhandledException;
29
30            InitializeComponent();
31        }
32
33        private void Application_Startup(object sender, StartupEventArgs e)
34        {
35            Bootstrapper bootstrapper = new Bootstrapper();
36            bootstrapper.Run();
37        }
38
39        private void Application_Exit(object sender, EventArgs e)
40        {
41        }
42
43        private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
44        {
45            // If the app is running outside of the debugger then report the exception using
46            // the browser's exception mechanism. On IE this will display it a yellow alert 
47            // icon in the status bar and Firefox will display a script error.
48            if (!System.Diagnostics.Debugger.IsAttached)
49            {
50                // NOTE: This will allow the application to continue running after an exception has been thrown
51                // but not handled. 
52                // For production applications this error handling should be replaced with something that will 
53                // report the error to the website and stop the application.
54                e.Handled = true;
55                Deployment.Current.Dispatcher.BeginInvoke(delegate { ReportErrorToDOM(e); });
56            }
57        }
58
59        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
60        private static void ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e)
61        {
62            try
63            {
64                string errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace;
65                errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n");
66
67                System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(\"Unhandled Error in Silverlight 2 Application " + errorMsg + "\");");
68            }
69            catch (Exception)
70            {
71            }
72        }
73    }
74}