view SilverlightValidation/SilverlightGlimpse/Services/GlimpseService.cs @ 59:3591c26bd63e

MVVMLight added
author Steven Hollidge <stevenhollidge@hotmail.com>
date Sat, 21 Apr 2012 19:20:28 +0100
parents 241e2f22ed3c
children
line wrap: on
line source

using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using SilverlightGlimpse.Controls;
using SilverlightGlimpse.Models;
using System.Windows.Media;

namespace SilverlightGlimpse.Services
{
    public class GlimpseService
    {
        #region Fields

        private static GlimpseService _instance;

        #endregion

        #region Private constructor

        private GlimpseService()
        {
        }

        #endregion

        #region Properties

        public static GlimpseService CreateInstance
        {
            get { return (_instance == null) ? _instance = new GlimpseService() : _instance; }
        }

        internal Application App { get; private set; }
        internal ChildWindow GlimpseWindow { get; set;}
        internal string HostApplicationName { get; set; }
        internal ObservableCollection<ExceptionWrapper> HostExceptions { get; private set; }
        internal FrameworkElement RootVisual { get; private set; }

        #endregion

        #region Creation and Loading

        public void DisplayLoadFailure(Application app, Exception ex, string hostApplicationName)
        {
            Debug.WriteLine("{0} had exception. {1}", this.HostApplicationName, ex.ToString());
            App = app;
            App.UnhandledException += Application_UnhandledException;
            RootVisual = new LoadExceptionViewer(ex, hostApplicationName);
            RootVisual.BindingValidationError += HostRootVisual_BindingValidationError;
        }

        public void Load(Application app, string hostApplicationName)
        {
            this.App = app;
            this.RootVisual = App.RootVisual as FrameworkElement;
            this.HostApplicationName = hostApplicationName;

            RootVisual.BindingValidationError += HostRootVisual_BindingValidationError;
            App.UnhandledException += Application_UnhandledException;

            var window = new ChildWindow() 
            { 
                Title = this.HostApplicationName,
                Content = new GlimpseViewer()
            };
            window.Show();
        }
        #endregion

        #region Events handlers

        private void HostRootVisual_BindingValidationError(object sender, ValidationErrorEventArgs e)
        {
            string controlName = "(none)";
            Control control = e.OriginalSource as Control;

            if (control != null && !string.IsNullOrEmpty(control.Name))
            {
                controlName = control.Name;
            }

            Exception ex = e.Error.Exception;

            while (ex != null)
            {
                this.HostExceptions.Add(new ExceptionWrapper(e.Action, controlName, e.Error.Exception));
                ex = ex.InnerException;
            }
        }

        private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {
            Debug.WriteLine("{0} had exception.  {1}", this.HostApplicationName, e.ExceptionObject.ToString());

            Exception ex = e.ExceptionObject;

            while (ex != null)
            {
                this.HostExceptions.Add(new ExceptionWrapper(ex));
                ex = ex.InnerException;
            }

            e.Handled = true;
        }

        #endregion
    }
}