Friday, 17 August 2012

Obelisk Windows 8 & WP7 MVVM Persistence Library V2.2 Release

I've just finished the Obelisk V2.2 release. It's now got support for Windows 8 persistence as well as WP7 persistence. There is a basic MVVM pattern example and an MVVM Light example for WP7 and Windows 8 The MSI only contains source for WP7 because I can't create an MSI in VS2012 yet, so you need to download the source to get the samples:

Tuesday, 7 August 2012

Windows 8 Audio Player Demo


I need to include a podcast player in the app I'm writing, so I decided to write a prototype app to demo the Windows 8 audio capabilities

Source here:


I wanted to include the following:

  • Background capabilities
  • Live tile glyph updates
  • A custom re-templatable control
  • Hardware interaction
  • Fully working demo with all required tasks

Background Audio

I've previously use the Background Audio Agent in WP7 and was expecting something similar. There's no direct equivalent but the MediaElement is Windows 8 provides new features different to Silverlight. The new MediaElement has an 'AudioCategory' attribute which can be set to 'BackgroundCapableMedia' to allow it to continue playing while the page is not active or when the app is suspended, but not closed.

<MediaElement x:Name="mediaElement" AudioCategory="BackgroundCapableMedia" AutoPlay="False"/>

The manifest needs modifying to have a Background declaration with 'Audio' and 'Control channel' properties:

Additionally, a wide tile must be used.

Live Tile Glyph Updates

I wasn't sure at first if this was done automatically by the task, but it's not, it needs doing manually, so I wrote a helper class to do it:

public enum Glyphs

public class BadgeHelper
  public static void UpdateBadge(Glyphs glyph)
    // Get badge xml content
    var content = BadgeUpdateManager.GetTemplateContent(Windows.UI.Notifications.BadgeTemplateType.BadgeGlyph);
    var element = content.GetElementsByTagName("badge")[0];

    // Set new glyph value
    element.Attributes[0].NodeValue = glyph.ToString();

    // Update badge
    var notification = new BadgeNotification(content);

Normal tile:

Tile with play glyph:

Custom Template

This is the same as Silverlight and WPF, I put in some visual states for play and paused mode. The template is in the default Templates\Generic.xaml location.

Hardware Interaction

The MediaControl object has a number of events which can be subscribed to.

The Demo

[Edit] The demo is now finished, enjoy!

Monday, 6 August 2012

Windows 8 MVVM Animation Control


As we know Windows 8 Metro apps (or whatever they are called now) don't support trigger or behaviours like Silverlight and WPF apps do, even though they are XAML family. This means that once you have crafted an animation in XAML, you can't actually trigger it without code behind. This article demonstrates how to use an attached property which is toggled by a bool dependency property and controls a pair of animations.


I have a common settings page which is opened via the charm bar which I was originally showing and hiding using a bool property in the view model and a visibility converter. I wanted to have an animation so that the control would slide in and out but wanted to control it through the view model.

Dependency Property

This dependency property is fairly straight forward, it has 3 dependency properties, the first binds to a boolean property and the other two bind to a pair of opposing animations to control the slide in and out:

/// <summary>
/// This attached property binds to a bool and a pair of StoryboardAnimations
/// When the bool is true, the true animation is started
/// When the bool is false, the fals animation is started
/// </summary>
public class BoolBeginStoryBoardTrigger
  #region Bool Property

  public static readonly DependencyProperty BoolProperty =
    DependencyProperty.RegisterAttached("Bool", typeof(bool),   typeof(BoolBeginStoryBoardTrigger), new PropertyMetadata(false, BoolPropertyChanged));

  public static void SetBool(DependencyObject attached, bool value)
    attached.SetValue(BoolProperty, value);

  public static bool GetBool(DependencyObject attached)
    return (bool)attached.GetValue(BoolProperty);

  private static void BoolPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    if (GetBool(d))


  #region TrueStoryboard Property

  public static readonly DependencyProperty TrueStoryboardProperty =
    DependencyProperty.RegisterAttached("TrueStoryboard", typeof(Storyboard), typeof(BoolBeginStoryBoardTrigger), null);

  public static void SetTrueStoryboard(DependencyObject attached, Storyboard value)
    attached.SetValue(TrueStoryboardProperty, value);

  public static Storyboard GetTrueStoryboard(DependencyObject attached)
    return (Storyboard)attached.GetValue(TrueStoryboardProperty);


  #region FalseStoryboard Property

  public static readonly DependencyProperty FalseStoryboardProperty =
  DependencyProperty.RegisterAttached("FalseStoryboard", typeof(Storyboard), typeof(BoolBeginStoryBoardTrigger), null);

  public static void SetFalseStoryboard(DependencyObject attached, Storyboard value)
    attached.SetValue(FalseStoryboardProperty, value);

  public static Storyboard GetFalseStoryboard(DependencyObject attached)
    return (Storyboard)attached.GetValue(FalseStoryboardProperty);



The attached property can be put anywhere after where the animations are defined. The 3 properties are set like this:

<Grid Style="{StaticResource LayoutRootStyle}"
        cmd:BoolBeginStoryBoardTrigger.Bool="{Binding IsSettingsVisible}"
        cmd:BoolBeginStoryBoardTrigger.FalseStoryboard="{StaticResource SettingsSlideIn}"
        cmd:BoolBeginStoryBoardTrigger.TrueStoryboard="{StaticResource             SettingsSlideOut}">

If you don't want to bind it to a View Model, it can easily bound to a toggle button in the view like this:

<Grid x:Name="settingsPanel"
          local:BoolBeginStoryBoardTrigger.Bool="{Binding ElementName=toggleButton, Path=IsChecked}"
          local:BoolBeginStoryBoardTrigger.FalseStoryboard="{StaticResource SettingsSlideIn}"
          local:BoolBeginStoryBoardTrigger.TrueStoryboard="{StaticResource SettingsSlideOut}"

Complete Settings Panel XAML

This is bound to the Settings view model which controls the IsSettingsVisible property.

    Width="346" HorizontalAlignment="Right"
    x:Name="ThisControl" Visibility="Collapsed"
    DataContext="{Binding Settings, Source={StaticResource Locator}}">
        <TranslateTransform x:Name="settingsTranslateX" X="346" />

        <Storyboard x:Key="SettingsSlideOut">
                <EasingDoubleKeyFrame KeyTime="0:0:0" Value="346" />
                <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0" >
                        <PowerEase EasingMode="EaseOut"/>
                <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="Visible" />                  

        <Storyboard x:Key="SettingsSlideIn">
                <EasingDoubleKeyFrame KeyTime="0:0:0" Value="0" />
                <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="346" >
                        <PowerEase EasingMode="EaseIn"/>

    <Grid Style="{StaticResource LayoutRootStyle}"
        cmd:BoolBeginStoryBoardTrigger.Bool="{Binding IsSettingsVisible}"
        cmd:BoolBeginStoryBoardTrigger.FalseStoryboard="{StaticResource SettingsSlideIn}"
        cmd:BoolBeginStoryBoardTrigger.TrueStoryboard="{StaticResource SettingsSlideOut}">
            <RowDefinition Height="140"/>
            <RowDefinition Height="*"/>

        <!-- Back button and page title -->
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            <ToggleButton  Style="{StaticResource BackToggleButtonStyle}"
                           IsChecked="{Binding IsSettingsVisible, Mode=TwoWay}"/>
            <TextBlock x:Name="pageTitle" Text="{Binding SettingsTitle}" Style="{StaticResource PageSubheaderTextStyle}" Grid.Column="1"/>

        <Grid Grid.Row="1" Margin="40,0,20,0">
                <RowDefinition Height="Auto" />
                <RowDefinition Height="20" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />

            <TextBlock Text="{Binding PushNotificationsText, Mode=OneWay}" VerticalAlignment="Center"
                       Style="{StaticResource TitleTextStyle}" />
            <ToggleSwitch Grid.Column="1" IsOn="{Binding IsPushEnabled, Mode=TwoWay}" />

            <TextBlock Grid.Row="2" Text="{Binding FontSizeText, Mode=OneWay}" Style="{StaticResource TitleTextStyle}" />
            <ComboBox Grid.Row="2" Grid.Column="1" ItemsSource="{Binding FontSizes}" DisplayMemberPath="Key"
                                SelectedItem="{Binding SelectedFontSize, Mode=TwoWay}" 
                                Grid.ColumnSpan="2"  >

Settings Service

The next article discusses how to implement the settings service.