Microsoft® Surface® Live Stream Code Sample

ive Stream enables businesses to engage their customers face-to-face using the most recent and relevant Twitter™, Flickr®, and RSS newsfeeds.

C# (5.9 MB)
 
 
 
 
 
4.3 Star
(4)
6,627 times
Add to favorites
4/19/2011
E-mail Twitter del.icio.us Digg Facebook

Solution explorer

C#
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Globalization;
using System.Threading;
using System.Windows;
using System.Windows.Threading;
using LiveStream.Properties;
using Microsoft.Practices.EnterpriseLibrary.Logging;

namespace LiveStream
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        /// <summary>
        /// A global random number generator for the app.
        /// </summary>
        internal static readonly Random Random = new Random();

        /// <summary>
        /// Initializes a new instance of the <see cref="App"/> class.
        /// </summary>
        /// <exception cref="T:System.InvalidOperationException">
        /// More than one instance of the <see cref="T:System.Windows.Application"/> class is created per <see cref="T:System.AppDomain"/>.
        /// </exception>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Justification = "Unlikely to be an issue.")]
        public App()
        {
            // This lets you change the culture in the Region and Language control panel and render the right culture.
            Thread.CurrentThread.CurrentCulture = CultureInfo.CurrentCulture;
            Thread.CurrentThread.CurrentUICulture = CultureInfo.CurrentCulture;

            DispatcherUnhandledException += App_DispatcherUnhandledException;
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

            ValidateSettings();
        }

        /// <summary>
        /// Validates the settings.
        /// </summary>
        private static void ValidateSettings()
        {
            if (Settings.Default.MinImageSize.Width > Settings.Default.MaxImageSize.Width ||
                Settings.Default.MinImageSize.Height > Settings.Default.MaxImageSize.Height ||
                Settings.Default.MinNewsSize.Width > Settings.Default.MaxNewsSize.Width ||
                Settings.Default.MinNewsSize.Height > Settings.Default.MaxNewsSize.Height ||
                Settings.Default.MinStatusSize.Width > Settings.Default.MaxStatusSize.Width ||
                Settings.Default.MinStatusSize.Height > Settings.Default.MaxStatusSize.Height)
            {
                string error = "The minimum sizes in the settings file must be smaller than the maximum sizes.";
                MessageBox.Show(error);
                throw new InvalidOperationException(error);
            }
        }

        /// <summary>
        /// Handles the DispatcherUnhandledException event of the App control. Logs crashes.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Threading.DispatcherUnhandledExceptionEventArgs"/> instance containing the event data.</param>
        private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            // http://stackoverflow.com/questions/2732140/how-do-i-catch-this-wpf-bitmap-loading-exception
            if (e.Exception.StackTrace.Contains("BitmapFrameDecode.get_ColorContexts"))
            {
                e.Handled = true;
                return;
            }

            if (e.Exception.Message != null && e.Exception.Message.Contains("No information was found about this pixel format"))
            {
                e.Handled = true;
                return;
            }

            LogCrash(e.Exception);
            throw e.Exception;
        }

        /// <summary>
        /// Handles the UnhandledException event of the CurrentDomain control. Logs crashes.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.UnhandledExceptionEventArgs"/> instance containing the event data.</param>
        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            LogCrash(e.ExceptionObject);
            throw e.ExceptionObject as Exception;
        }

        /// <summary>
        /// Logs the crash.
        /// </summary>
        /// <param name="exceptionObject">The exception object.</param>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters", MessageId = "Microsoft.Practices.EnterpriseLibrary.Logging.LogEntry.set_Message(System.String)", Justification = "It's just a log message.")]
        private static void LogCrash(object exceptionObject)
        {
            LogEntry logEntry = new LogEntry();
            logEntry.Categories.Add("Crash");
            logEntry.Message = exceptionObject.ToString();
            logEntry.Message += Environment.NewLine + "Memory: " + Process.GetCurrentProcess().PrivateMemorySize64;
            logEntry.TimeStamp = DateTime.Now;
            Logger.Write(logEntry);
        }

        /// <summary>
        /// Raises the <see cref="E:System.Windows.Application.Exit"/> event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.Windows.ExitEventArgs"/> that contains the event data.</param>
        protected override void OnExit(ExitEventArgs e)
        {
            Settings.Default.Save();
            base.OnExit(e);
        }
    }
}