PageRenderTime 16ms CodeModel.GetById 9ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/V4/MVVM RI/MVVM.Client.Tests/App.xaml.cs

#
C# | 75 lines | 44 code | 8 blank | 23 comment | 1 complexity | 0142aa833d3a6e86c429c35cf3f2dced 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;
19using Microsoft.Silverlight.Testing;
20
21namespace MVVM.Client.Tests
22{
23    public partial class App : Application
24    {
25
26        public App()
27        {
28            this.Startup += this.Application_Startup;
29            this.Exit += this.Application_Exit;
30            this.UnhandledException += this.Application_UnhandledException;
31
32            InitializeComponent();
33        }
34
35        private void Application_Startup(object sender, StartupEventArgs e)
36        {
37            this.RootVisual = UnitTestSystem.CreateTestPage();
38        }
39
40        private void Application_Exit(object sender, EventArgs e)
41        {
42        }
43
44        private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
45        {
46            // If the app is running outside of the debugger then report the exception using
47            // the browser's exception mechanism. On IE this will display it a yellow alert 
48            // icon in the status bar and Firefox will display a script error.
49            if (!System.Diagnostics.Debugger.IsAttached)
50            {
51
52                // NOTE: This will allow the application to continue running after an exception has been thrown
53                // but not handled. 
54                // For production applications this error handling should be replaced with something that will 
55                // report the error to the website and stop the application.
56                e.Handled = true;
57                Deployment.Current.Dispatcher.BeginInvoke(delegate { this.ReportErrorToDOM(e); });
58            }
59        }
60
61        private void ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e)
62        {
63            try
64            {
65                string errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace;
66                errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n");
67
68                System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(\"Unhandled Error in Silverlight Application " + errorMsg + "\");");
69            }
70            catch (Exception)
71            {
72            }
73        }
74    }
75}