Skip to content

Instantly share code, notes, and snippets.

@kellabyte
Created June 19, 2012 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kellabyte/2956696 to your computer and use it in GitHub Desktop.
Save kellabyte/2956696 to your computer and use it in GitHub Desktop.
using System;
using System.Windows;
using System.Windows.Media;
using Microsoft.Phone.Controls;
using VideoDemo.Messaging;
namespace VideoDemo
{
public partial class MainPage : PhoneApplicationPage,
IHandle<PlayMessage>,
IHandle<PauseMessage>
{
private EventAggregator events;
private TimeSpan duration;
public MainPage()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
this.buttonPlay.Click +=
new RoutedEventHandler(buttonPlay_Click);
this.buttonPause.Click +=
new RoutedEventHandler(buttonPause_Click);
media.CurrentStateChanged +=
new RoutedEventHandler(media_CurrentStateChanged);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
events = App.Container.Resolve<EventAggregator>l;();
events.Init();
events.Subscribe(this);
media.Source = new Uri("http://someurl/tron.wmv");
}
void media_CurrentStateChanged(object sender, RoutedEventArgs e)
{
switch (media.CurrentState)
{
case MediaElementState.Playing:
media.Position = duration;
break;
}
}
void buttonPlay_Click(object sender, RoutedEventArgs e)
{
// Here we publish the interaction as an event to Azure
// ServiceBus. We don't do any mutation of state without
// the event going through our event mechanism. This
// ensures all our code when event sourced from Azure
// ServiceBus runs as-is with the native behaviors of the app.
events.Publish(new PlayMessage(media.Position));
}
void buttonPause_Click(object sender, RoutedEventArgs e)
{
// Same as above.
events.Publish(new PauseMessage(media.Position));
}
public void Handle(PlayMessage message)
{
// Since we are handling the even there we can mutate
// state. Live interactions or event sourced events from
// Azure ServiceBus will all go through this path. It is the
// native behavior of the app.
duration = message.Duration;
media.Play();
}
public void Handle(PauseMessage message)
{
// Same as above.
duration = message.Duration;
media.Pause();
}
}
}
// Here are the message types.
public class PlayMessage : Message
{
public TimeSpan Duration { get; set; }
public PlayMessage()
{
}
public PlayMessage(TimeSpan duration)
{
this.Duration = duration;
}
}
public class PauseMessage : Message
{
public TimeSpan Duration { get; set; }
public PauseMessage()
{
}
public PauseMessage(TimeSpan duration)
{
this.Duration = duration;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment