PageRenderTime 21ms CodeModel.GetById 16ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/Source/Bifrost.Mimir/App.xaml.cs

#
C# | 85 lines | 65 code | 13 blank | 7 comment | 1 complexity | 8d6a2dff9121cf4b13a54983659ee124 MD5 | raw file
 1using System;
 2using System.Windows;
 3using Bifrost.Configuration.Defaults;
 4using Bifrost.Execution;
 5using Bifrost.Ninject;
 6using Bifrost.Notification;
 7using Microsoft.Practices.ServiceLocation;
 8using Ninject;
 9
10namespace Bifrost.Mimir
11{
12    public partial class App
13	{
14		public static IContainer Container;
15
16		static App()
17		{
18            var dispatcher = new Dispatcher(Deployment.Current.Dispatcher);
19            DispatcherManager.Current = dispatcher;
20		}
21
22		public App()
23		{
24			Startup += Application_Startup;
25			Exit += Application_Exit;
26			UnhandledException += Application_UnhandledException;
27
28            var kernel = new StandardKernel();
29            Container = new Container(kernel);
30            var serviceLocator = new ContainerServiceLocator(Container);
31            Container.Bind<IServiceLocator>(serviceLocator);
32            ServiceLocator.SetLocatorProvider(() => serviceLocator);
33
34            var bindings = new DefaultBindings();
35            bindings.Initialize(Container);
36
37            var conventions = new DefaultConventions();
38            conventions.Initialize();
39
40			InitializeComponent();
41		}
42
43		private void Application_Startup(object sender, StartupEventArgs e)
44		{
45            var path = Application.Current.Host.Source.AbsolutePath;
46			RootVisual = new MainPage();
47		}
48
49		private void Application_Exit(object sender, EventArgs e)
50		{
51
52		}
53
54		private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
55		{
56			// If the app is running outside of the debugger then report the exception using
57			// the browser's exception mechanism. On IE this will display it a yellow alert 
58			// icon in the status bar and Firefox will display a script error.
59			if (!System.Diagnostics.Debugger.IsAttached)
60			{
61
62				// NOTE: This will allow the application to continue running after an exception has been thrown
63				// but not handled. 
64				// For production applications this error handling should be replaced with something that will 
65				// report the error to the website and stop the application.
66				e.Handled = true;
67				Deployment.Current.Dispatcher.BeginInvoke(delegate { ReportErrorToDOM(e); });
68			}
69		}
70
71		private void ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e)
72		{
73			try
74			{
75				string errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace;
76				errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n");
77
78				System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(\"Unhandled Error in Silverlight Application " + errorMsg + "\");");
79			}
80			catch (Exception)
81			{
82			}
83		}
84	}
85}