Skip to main content

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,676 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.Linq;
using System.Windows;
using FeedProcessor.Enums;
using FeedProcessor.FeedItems;
using LiveStream.Helpers;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Controls;

namespace LiveStream.Controls.RiverContent
{
    /// <summary>
    /// A river item which shows the large version of images.
    /// </summary>
    public partial class LargeImage : SurfaceUserControl
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="LargeImage"/> class.
        /// </summary>
        public LargeImage()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Determines the size of the image to load and displays it.
        /// </summary>
        internal void RenderContent()
        {
            ImageFeedItem image = DataContext as ImageFeedItem;
            _viewport.Reset();
            if (image == null)
            {
                _image.UriSource = null;
                return;
            }

            if (image.SourceType == SourceType.Flickr)
            {
                ImageFeedItem.GetFlickrImageSizes(
                    image,
                    Constants.FlickrApiKey,
                    new GetFlickrImageSizesCallback((imageFeedItem) => SetImageSource()));
            }
            else
            {
                SetImageSource();
            }
        }

        /// <summary>
        /// When the image sizes are loaded, set the image source to the largest image available.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity", Justification = "This is really so complex?")]
        private void SetImageSource()
        {
            ImageFeedItem image = DataContext as ImageFeedItem;
            if (image == null)
            {
                return;
            }

            _viewport.ScatterViewItem = this.FindVisualParent<ScatterViewItem>();

            if (image.SourceType == SourceType.Twitter || image.Sizes == null)
            {
                _image.UriSource = image.ThumbnailUri;
                return;
            }

            Uri original = (from kvp in image.Sizes where kvp.Key.Width > 1024 || kvp.Key.Height > 1024 select kvp.Value).FirstOrDefault();
            Uri large = (from kvp in image.Sizes where kvp.Key.Width == 1024 || kvp.Key.Height == 1024 select kvp.Value).FirstOrDefault();
            Uri medium = (from kvp in image.Sizes where kvp.Key.Width == 500 || kvp.Key.Height == 500 select kvp.Value).FirstOrDefault();

            Uri chosen = null;

            if (large == null && original != null)
            {
                chosen = original;
            }
            else if (large != null)
            {
                chosen = large;
            }
            else if (medium != null)
            {
                chosen = medium;
            }

            _image.UriSource = chosen != null ? chosen : image.ThumbnailUri;
        }

        /// <summary>
        /// Handles the ContactTapGesture event of the Close button.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Microsoft.Surface.Presentation.ContactEventArgs"/> instance containing the event data.</param>
        private void Close_ContactTapGesture(object sender, ContactEventArgs e)
        {
            RaiseEvent(new UserSourceRoutedEventArgs(RiverItemBase.CloseRequestedEvent, _closeBtn));
        }

        /// <summary>
        /// Handles the ContactTapGesture event of the Flip button.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Microsoft.Surface.Presentation.ContactEventArgs"/> instance containing the event data.</param>
        private void Flip_ContactTapGesture(object sender, ContactEventArgs e)
        {
            RaiseEvent(new UserSourceRoutedEventArgs(RiverItemBase.FlipRequestedEvent, _flipBtn));
        }

        /// <summary>
        /// Handles the IsAnyContactCapturedWithinChanged event of the Viewport control. Shows the manipulation border.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Event handler is added in XAML.")]
        private void Viewport_IsAnyContactCapturedWithinChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            VisualStateManager.GoToState(this, _viewport.IsAnyContactCapturedWithin ? IsManipulatingState.Name : NotManipulatingState.Name, true);
        }
    }
}