59
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
1 using System;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
2 using System.Collections.ObjectModel;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
3 using System.Diagnostics;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
4 using System.Windows;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
5 using System.Windows.Controls;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
6 using SilverlightGlimpse.Controls;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
7 using SilverlightGlimpse.Models;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
8
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
9 namespace SilverlightGlimpse.Services
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
10 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
11 public class GlimpseService
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
12 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
13 #region Fields
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
14
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
15 private static GlimpseService _instance;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
16
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
17 #endregion
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
18
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
19 #region Private constructor
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
20
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
21 private GlimpseService()
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
22 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
23 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
24
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
25 #endregion
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
26
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
27 #region Properties
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
28
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
29 public static GlimpseService CreateInstance
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
30 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
31 get { return (_instance == null) ? _instance = new GlimpseService() : _instance; }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
32 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
33
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
34 internal Application App { get; private set; }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
35 internal FloatableWindow GlimpseWindow { get; set;}
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
36 internal string HostApplicationName { get; set; }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
37 internal ObservableCollection<ExceptionWrapper> HostExceptions { get; private set; }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
38 internal FrameworkElement RootVisual { get; private set; }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
39
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
40 #endregion
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
41
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
42 #region Creation and Loading
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
43
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
44 public void DisplayLoadFailure(Application app, Exception ex, string hostApplicationName)
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
45 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
46 Debug.WriteLine("{0} had exception. {1}", this.HostApplicationName, ex.ToString());
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
47 App = app;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
48 RootVisual = new LoadExceptionViewer(ex, hostApplicationName);
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
49
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
50 RootVisual.BindingValidationError += HostRootVisual_BindingValidationError;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
51 App.UnhandledException += Application_UnhandledException;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
52 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
53
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
54 public void Load(Application app, string hostApplicationName)
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
55 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
56 this.App = app;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
57 this.RootVisual = App.RootVisual as FrameworkElement;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
58 this.HostApplicationName = hostApplicationName;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
59
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
60 RootVisual.BindingValidationError += HostRootVisual_BindingValidationError;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
61 App.UnhandledException += Application_UnhandledException;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
62
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
63 FloatableWindow window = new FloatableWindow();
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
64 window.Title = this.HostApplicationName;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
65 window.Content = new GlimpseViewer();
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
66
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
67 if (Double.IsNaN(this.RootVisual.Width))
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
68 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
69 //if the host control is autosized (consumes the browser window) then locate Glimpse in the top, left
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
70 window.Show();
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
71 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
72 else
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
73 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
74 //if the host is fixed size then attempt to locate the popup control in the upper right corner
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
75 var dblLeft = this.RootVisual.Width - 200;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
76
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
77 if (dblLeft < 0)
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
78 dblLeft = 0;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
79
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
80 window.Show();
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
81 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
82 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
83 #endregion
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
84
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
85 #region Events handlers
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
86
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
87 private void HostRootVisual_BindingValidationError(object sender, ValidationErrorEventArgs e)
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
88 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
89 string controlName = "(none)";
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
90 Control control = e.OriginalSource as Control;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
91
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
92 if (control != null && !string.IsNullOrEmpty(control.Name))
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
93 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
94 controlName = control.Name;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
95 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
96
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
97 Exception ex = e.Error.Exception;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
98
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
99 while (ex != null)
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
100 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
101 this.HostExceptions.Add(new ExceptionWrapper(e.Action, controlName, e.Error.Exception));
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
102 ex = ex.InnerException;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
103 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
104 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
105
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
106 private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
107 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
108 Debug.WriteLine("{0} had exception. {1}", this.HostApplicationName, e.ExceptionObject.ToString());
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
109
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
110 Exception ex = e.ExceptionObject;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
111
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
112 while (ex != null)
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
113 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
114 this.HostExceptions.Add(new ExceptionWrapper(ex));
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
115 ex = ex.InnerException;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
116 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
117
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
118 e.Handled = true;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
119 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
120
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
121 #endregion
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
122 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
123 } |