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

MVVMLight added
author Steven Hollidge <stevenhollidge@hotmail.com>
date Sat, 21 Apr 2012 19:20:28 +0100
parents
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;

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 FloatableWindow 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;
            RootVisual = new LoadExceptionViewer(ex, hostApplicationName);

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

        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;

            FloatableWindow window = new FloatableWindow();
            window.Title = this.HostApplicationName;
            window.Content = new GlimpseViewer();

            if (Double.IsNaN(this.RootVisual.Width))
            {
                //if the host control is autosized (consumes the browser window) then locate Glimpse in the top, left
                window.Show();
            }
            else
            {
                 //if the host is fixed size then attempt to locate the popup control in the upper right corner
                var dblLeft = this.RootVisual.Width - 200;

                if (dblLeft < 0)
                    dblLeft = 0;

                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
    }
}