view 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
line wrap: on
line source

Imports System.Diagnostics
Imports System.Collections.ObjectModel
Imports System.ComponentModel

Public Class GlimpseService

#Region " Declarations "

    Private Shared _instance As GlimpseService
    Private _objGlimpseWindow As ChildWindow
    Private _objHostExceptions As New ObservableCollection(Of ExceptionWrapper)
    Private _strHostApplicationName As String = String.Empty
    Private WithEvents _objApp As Application
    Private WithEvents _objRootVisual As FrameworkElement

#End Region

#Region " Properties "

    Public Shared ReadOnly Property CreateInstance() As GlimpseService
        Get

            If _instance Is Nothing Then
                _instance = New GlimpseService
            End If

            Return _instance
        End Get
    End Property

    Friend Property App() As Application
        Get
            Return _objApp
        End Get
        Set(ByVal Value As Application)
            _objApp = Value
        End Set
    End Property

    Friend Property GlimpseWindow() As ChildWindow
        Get
            Return _objGlimpseWindow
        End Get

        Private Set(ByVal Value As ChildWindow)
            _objGlimpseWindow = Value
        End Set
    End Property

    Friend Property HostApplicationName() As String
        Get
            Return _strHostApplicationName
        End Get
        Set(ByVal Value As String)
            _strHostApplicationName = Value
        End Set
    End Property

    Friend ReadOnly Property HostExceptions() As ObservableCollection(Of ExceptionWrapper)
        Get
            Return _objHostExceptions
        End Get
    End Property

    Friend Property RootVisual() As FrameworkElement
        Get
            Return _objRootVisual
        End Get
        Set(ByVal Value As FrameworkElement)
            _objRootVisual = Value
        End Set
    End Property

#End Region

#Region " Creation and Loading "

    Public Sub DisplayLoadFailure(ByVal objApp As Application, ByVal ex As Exception, ByVal strHostApplicationName As String)
        Debug.WriteLine(String.Format("{0} had exception. {1}", Me.HostApplicationName, ex.ToString))
        _objApp = objApp
        _objApp.RootVisual = New LoadExceptionViewer(ex, strHostApplicationName)
    End Sub

    Public Sub Load(ByVal objApp As Application, ByVal strHostApplicationName As String)
        Me.App = objApp
        Me.RootVisual = TryCast(objApp.RootVisual, FrameworkElement)
        Me.HostApplicationName = strHostApplicationName

        Dim fw As New ChildWindow
        fw.Title = Me.HostApplicationName
        fw.Content = New GlimpseViewer

        If Double.IsNaN(Me.RootVisual.Width) Then
            'if the host control is autosized (consumes the browser window) then locate Glimpse in the top, left
            fw.Show()

        Else

            'if the host is fixed size then attempt to locate the popup control in the upper right corner
            Dim dblLeft As Double = Me.RootVisual.Width - 200

            If dblLeft < 0 Then
                dblLeft = 0
            End If

            fw.Show()
        End If

    End Sub

    Private Sub New()
    End Sub

#End Region

#Region " Host Application Events "

    Private Sub _objHostRootVisual_BindingValidationError(ByVal sender As Object, ByVal e As System.Windows.Controls.ValidationErrorEventArgs) Handles _objRootVisual.BindingValidationError

        Dim strControlName As String = "(none)"
        Dim objControl As Control = TryCast(e.OriginalSource, Control)

        If objControl IsNot Nothing AndAlso Not String.IsNullOrEmpty(objControl.Name) Then
            strControlName = objControl.Name
        End If

        Dim ex As Exception = e.Error.Exception

        While ex IsNot Nothing
            Me.HostExceptions.Add(New ExceptionWrapper(e.Action, strControlName, e.Error.Exception))
            ex = ex.InnerException
        End While

    End Sub

    Private Sub Application_UnhandledException(ByVal sender As Object, ByVal e As ApplicationUnhandledExceptionEventArgs) Handles _objApp.UnhandledException
        Debug.WriteLine(String.Format("{0} had exception.  {1}", Me.HostApplicationName, e.ExceptionObject.ToString))

        Dim ex As Exception = e.ExceptionObject

        While ex IsNot Nothing
            Me.HostExceptions.Add(New ExceptionWrapper(ex))
            ex = ex.InnerException
        End While

        e.Handled = True
    End Sub

#End Region

End Class