comparison Glimpse/Glimpse Services/GlimpseService.vb @ 59:3591c26bd63e

MVVMLight added
author Steven Hollidge <stevenhollidge@hotmail.com>
date Sat, 21 Apr 2012 19:20:28 +0100
parents
children 536498832a79
comparison
equal deleted inserted replaced
58:241e2f22ed3c 59:3591c26bd63e
1 Imports System.Diagnostics
2 Imports System.Collections.ObjectModel
3 Imports System.ComponentModel
4
5 Public Class GlimpseService
6
7 #Region " Declarations "
8
9 Private Shared _instance As GlimpseService
10 Private _objGlimpseWindow As ChildWindow
11 Private _objHostExceptions As New ObservableCollection(Of ExceptionWrapper)
12 Private _strHostApplicationName As String = String.Empty
13 Private WithEvents _objApp As Application
14 Private WithEvents _objRootVisual As FrameworkElement
15
16 #End Region
17
18 #Region " Properties "
19
20 Public Shared ReadOnly Property CreateInstance() As GlimpseService
21 Get
22
23 If _instance Is Nothing Then
24 _instance = New GlimpseService
25 End If
26
27 Return _instance
28 End Get
29 End Property
30
31 Friend Property App() As Application
32 Get
33 Return _objApp
34 End Get
35 Set(ByVal Value As Application)
36 _objApp = Value
37 End Set
38 End Property
39
40 Friend Property GlimpseWindow() As ChildWindow
41 Get
42 Return _objGlimpseWindow
43 End Get
44
45 Private Set(ByVal Value As ChildWindow)
46 _objGlimpseWindow = Value
47 End Set
48 End Property
49
50 Friend Property HostApplicationName() As String
51 Get
52 Return _strHostApplicationName
53 End Get
54 Set(ByVal Value As String)
55 _strHostApplicationName = Value
56 End Set
57 End Property
58
59 Friend ReadOnly Property HostExceptions() As ObservableCollection(Of ExceptionWrapper)
60 Get
61 Return _objHostExceptions
62 End Get
63 End Property
64
65 Friend Property RootVisual() As FrameworkElement
66 Get
67 Return _objRootVisual
68 End Get
69 Set(ByVal Value As FrameworkElement)
70 _objRootVisual = Value
71 End Set
72 End Property
73
74 #End Region
75
76 #Region " Creation and Loading "
77
78 Public Sub DisplayLoadFailure(ByVal objApp As Application, ByVal ex As Exception, ByVal strHostApplicationName As String)
79 Debug.WriteLine(String.Format("{0} had exception. {1}", Me.HostApplicationName, ex.ToString))
80 _objApp = objApp
81 _objApp.RootVisual = New LoadExceptionViewer(ex, strHostApplicationName)
82 End Sub
83
84 Public Sub Load(ByVal objApp As Application, ByVal strHostApplicationName As String)
85 Me.App = objApp
86 Me.RootVisual = TryCast(objApp.RootVisual, FrameworkElement)
87 Me.HostApplicationName = strHostApplicationName
88
89 Dim fw As New ChildWindow
90 fw.Title = Me.HostApplicationName
91 fw.Content = New GlimpseViewer
92
93 If Double.IsNaN(Me.RootVisual.Width) Then
94 'if the host control is autosized (consumes the browser window) then locate Glimpse in the top, left
95 fw.Show()
96
97 Else
98
99 'if the host is fixed size then attempt to locate the popup control in the upper right corner
100 Dim dblLeft As Double = Me.RootVisual.Width - 200
101
102 If dblLeft < 0 Then
103 dblLeft = 0
104 End If
105
106 fw.Show()
107 End If
108
109 End Sub
110
111 Private Sub New()
112 End Sub
113
114 #End Region
115
116 #Region " Host Application Events "
117
118 Private Sub _objHostRootVisual_BindingValidationError(ByVal sender As Object, ByVal e As System.Windows.Controls.ValidationErrorEventArgs) Handles _objRootVisual.BindingValidationError
119
120 Dim strControlName As String = "(none)"
121 Dim objControl As Control = TryCast(e.OriginalSource, Control)
122
123 If objControl IsNot Nothing AndAlso Not String.IsNullOrEmpty(objControl.Name) Then
124 strControlName = objControl.Name
125 End If
126
127 Dim ex As Exception = e.Error.Exception
128
129 While ex IsNot Nothing
130 Me.HostExceptions.Add(New ExceptionWrapper(e.Action, strControlName, e.Error.Exception))
131 ex = ex.InnerException
132 End While
133
134 End Sub
135
136 Private Sub Application_UnhandledException(ByVal sender As Object, ByVal e As ApplicationUnhandledExceptionEventArgs) Handles _objApp.UnhandledException
137 Debug.WriteLine(String.Format("{0} had exception. {1}", Me.HostApplicationName, e.ExceptionObject.ToString))
138
139 Dim ex As Exception = e.ExceptionObject
140
141 While ex IsNot Nothing
142 Me.HostExceptions.Add(New ExceptionWrapper(ex))
143 ex = ex.InnerException
144 End While
145
146 e.Handled = True
147 End Sub
148
149 #End Region
150
151 End Class