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.ComponentModel;
using System.Diagnostics;
using System.Windows.Media.Animation;
using LiveStream.Helpers;
using LiveStream.Properties;
using Microsoft.Surface;
using Microsoft.Surface.Presentation.Controls;

namespace LiveStream
{
    /// <summary>
    /// The main application window.
    /// </summary>
    public partial class SurfaceWindow : Microsoft.Surface.Presentation.Controls.SurfaceWindow
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="SurfaceWindow"/> class.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Justification = "Not an issue.")]
        public SurfaceWindow()
        {
            InitializeComponent();

            // Add handlers for Application activation events
            ApplicationLauncher.ApplicationActivated += OnApplicationActivated;
            ApplicationLauncher.ApplicationPreviewed += OnApplicationPreviewed;
            ApplicationLauncher.ApplicationDeactivated += OnApplicationDeactivated;

            if (SurfaceEnvironment.IsInSingleAppMode && SurfaceEnvironment.IsRunningInShell)
            {
                /*
                 * http://community.surface.com/blogs/community_and_support_articles/archive/2009/10/28/kb-applicationactivated-event-is-never-raised-in-single-application-mode.aspx
                 * When a Surface unit is running multiple Surface applications, the Microsoft.Surface.ApplicationLauncher.ApplicationActivated
                 * event is raised when an application is activated and users can interact with it. However, in single-application mode, the
                 * ApplicationActivated event is not raised, so Surface applications that depend on ApplicationActivated might not work.
                 */
                Loaded += (sender, e) => Startup();
            }

            // Add the configuable color resources.
            Resources["ThemeColor"] = Settings.Default.ThemeColor;
            Resources["ForegroundColor"] = Settings.Default.ForegroundColor;

            // Don't put all the binding errors in the output window
            PresentationTraceSources.DataBindingSource.Switch.Level = SourceLevels.Critical;

            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                _layoutRoot.Opacity = 0;
            }
        }

        /// <summary>
        /// Raises the <see cref="E:System.Windows.Window.Closed"/> event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data.</param>
        protected override void OnClosed(EventArgs e)
        {
            ApplicationLauncher.ApplicationActivated -= OnApplicationActivated;
            ApplicationLauncher.ApplicationPreviewed -= OnApplicationPreviewed;
            ApplicationLauncher.ApplicationDeactivated -= OnApplicationDeactivated;

            base.OnClosed(e);
        }

        /// <summary>
        /// Set up the app state.
        /// </summary>
        private void Startup()
        {
            if (AppState.Instance.IsInitialized)
            {
                return;
            }

            Audio.Initialize();
            AppState.Instance.Initialize(_river);
            DataContext = AppState.Instance;

            // Fade the app in so that the loading message appears on the correct side before the user sees it.
            (Resources["ShowActivated"] as Storyboard).Begin(this);
        }

        /// <summary>
        /// Occurs when users can interact with the application.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void OnApplicationActivated(object sender, EventArgs e)
        {
            if (!AppState.Instance.IsInitialized)
            {
                Startup();
            }

            AppState.Instance.IsPaused = false;
        }

        /// <summary>
        /// Occurs when the application window is visible to users but users cannot interact with the application.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void OnApplicationPreviewed(object sender, EventArgs e)
        {
            if (SurfaceEnvironment.IsInSingleAppMode)
            {
                return;
            }

            AppState.Instance.IsPaused = true;
        }

        /// <summary>
        /// Occurs when the application is no longer visible to users in any way and should enter a low-CPU usage state, pause, and so on.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void OnApplicationDeactivated(object sender, EventArgs e)
        {
            if (SurfaceEnvironment.IsInSingleAppMode)
            {
                return;
            }

            AppState.Instance.IsPaused = true;
        }
    }
}