Hello World: .NET Framework 3.0 XAML Browser Applications

banner art

The following Hello World sample code will do the following:

  • Display a Windows Media Center dialog box using the Windows Media Center API.
  • Play a video in the Windows Media Center view port using the Windows Media Center API.

The Hello World example

XAML (markup)

<Page x:Class="myXbapMCEApp.Page1"
    xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
    Title="Page1" Background="{StaticResource PageGlassyBackground}"
    Loaded="OnLoaded" >

  <!— Margins to prevent Windows Media Center chrome to overlap App UI. -->
  <!— Allow remote control arrow keys to cycle focus through buttons. -->
  <StackPanel Margin="10,50,10,50"  KeyboardNavigation.DirectionalNavigation="Contained">

    <!— Event handler -->
    <Button Click="OnClickDialog" Width="280" Height="100" 
            VerticalAlignment="Center" HorizontalAlignment="Center"  
            Name="button1" VerticalContentAlignment="Center">Launch Dialog</Button>

    <!— Event handler -->
    <Button Click="OnClickVideo" Width="280" Height="100" 
            VerticalAlignment="Center" HorizontalAlignment="Center"  
            Name="button2" VerticalContentAlignment="Center">Play Video</Button>
  </StackPanel>
</Page>

Underlying code

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.MediaCenter;

namespace myXbapMCEApp
{
    /// <summary>
    /// Interaction logic for Page1.xaml.
    /// </summary>

    public partial class Page1 : Page
    {

        public Page1()
        {
            InitializeComponent();
        }

        void OnLoaded(object sender, RoutedEventArgs e)
        {
            button1.Focus();    // Set focus to 1st control. Can be done in XAML.
        }

        void OnClickDialog(object sender, RoutedEventArgs e)
        {
            MediaCenterEnvironment mce;
            Microsoft.MediaCenter.Hosting.AddInHost host = Microsoft.MediaCenter.Hosting.AddInHost.Current;
            mce = host.MediaCenterEnvironment;    // Get access to Windows Media Center host.
            // Launch the dialog box.
            mce.Dialog("Hello World!", "Windows Media Center dialog box called from XBAP",
                DialogButtons.Ok, 10, false);
        }

        void OnClickVideo(object sender, RoutedEventArgs e)
        {
            string url = @"c:\SampleFile.wmv";  // A sample file to play.
            MediaCenterEnvironment mce;
            Microsoft.MediaCenter.Hosting.AddInHost host = Microsoft.MediaCenter.Hosting.AddInHost.Current;
            mce = host.MediaCenterEnvironment;    // Get access to Windows Media Center host.
            // Play the video in the Windows Media Center view port.
            mce.PlayMedia(MediaType.Video, url, false);
        }
    }
}

Project properties

Note the reference to the Microsoft.MediaCenter.dll file. This DLL is currently not marked APTCA, so some of the Windows Media Center APIs can only run in full trust. Until this is fixed, applications must run in full trust.

MediaCenterTheme.XAML provides the control style (such as a flat look-and-feel UI) and is set by default by choosing "Windows Media Center Application (WPF)" when creating a new project in Visual Studio.

The project properties

See Also