• MCML : First Contact (again)

    Okay, so we have created a (really simple) menu control, the next step is to wrap it all in a reusable control that you could use in all your pages / in another project. Doing so will require only 2 things :

    • manage keyboard and mouse input
    • move all the mcml code to a new UI

    Moving the control to its own UI element is not really difficult, all we need to remember is to add properties for the needed data.

    <Mcml 
        xmlns="http://schemas.microsoft.com/2006/mcml"
        xmlns:me="Me"
        xmlns:cor="assembly://MScorLib/System">
        <UI Name="MenuPage">
            <Locals>
                <Choice Name="TheCommands">
                    <Options>
                        <cor:String String="Command1" />
                        <cor:String String="Command2" />
                        <cor:String String="Command3" />
                    </Options>
                </Choice>
            </Locals>
            <Content>
                <ColorFill Content="LightGray" Layout="Center">
                    <Children>
                        <me:MyMenu MenuItems="[TheCommands]" />
                    </Children>
                </ColorFill>
            </Content>
        </UI>
    
        <UI Name="MyMenu">
            <Properties>
                <Choice Name="MenuItems" Choice="$Required" />
            </Properties>
            <Content>
                <Repeater Layout="VerticalFlow" Source="[MenuItems.Options]">
                    <Content>
                        <me:MyMenuItem 
                            Title="[RepeatedItem!cor:String]"
                            MyIndex="[RepeatedItemIndex]"
                            Commands="[MenuItems]"/>
                    </Content>
                </Repeater>
            </Content>
        </UI>
    ...
    </Mcml>

    Nothing difficult here : just cut & paste the Repeater into a new UI, add a property of type Choice and add a "reference" to the newly created control. Now let's add a little interactivity, shall we ?

    As I have hinted in the previous post, you just add keyboard and mouse interaction by adding rules to your object. One of the fundamental rules is :

            <Default Target="[Input.KeyInteractive]" Value="true" />
    

    which enables a UI to be keyboard-focusable. The actual process of finding which control will be the next one to gain focus when you press a key is left to MediaCenter itself, so don't bother with it. Whenever an item gains focus, the KeyFocus property of Input is changed from false to true, so you can easily add a rule for event-handling.

            <Condition Source="[Input.KeyFocus]" SourceValue="true">
                <Actions>
                    <Set Target="[Commands.ChosenIndex]" Value="[MyIndex.Value]" />
                    <Set Target="[CommandLabel.Font]" Value="Segoe UI, 24, Bold" />
                    <Set Target="[CommandLabel.Alpha]" Value="1" />
                </Actions>
            </Condition>

    For the MyMenuItem class, we just change the selected item (the chosen index from our list of commands) and the formatting of our label. This code is a little bit different from the one I'd shown in my previous post, but I won't explain here what makes this code easier to read but not as powerful as the other one.

    If you look at the SDK documentation, you'll see that the InputElement as quite a few properties, some of them beeing really interesting :

    • KeyFocusOnMouseEnter can be set to false if you want the focus not to be automatically taken by a control on mouse-over
    • MouseFocus is set to true when the mouse is over an element. (combined with the previous element, it allows you to create a hover effect like an html A/A:hover css rule)
    • KeyDeepFocus is set to true if the element or one of its children have the keyboard-focus
    • MouseDeepFocus is the same but for mouse-focus

    Next thing to do : add a click effect to our menu-item (the process of actually doing something will be covered in a future post). This is a little bit more difficult, as there's no EnterPressed or MouseClicked property on the Input element... For this basic interactivity, we'll use a local ClickHandler object, which as its name suggest, just reacts to a "click" (left mouse button, space and enter) and add rules to its Clicking property.

    <UI Name="MyMenuItem">
        ...
        <Locals>
            <ClickHandler Name="ClickHandler"/>
        </Locals>
        <Rules>
            ...
            <Condition Source="[ClickHandler.Clicking]" SourceValue="true">
                <Actions>
                    <Set Target="[CommandLabel.Color]" Value="Red" />
                </Actions>
            </Condition>
        </Rules>
        ...
    </UI>

    Now, we'll make it really re-usable by creating a new mcml file, and moving the two controls in it :

    MyMenu.mcml
    <Mcml xmlns="http://schemas.microsoft.com/2006/mcml" xmlns:me="Me" xmlns:cor="assembly://MScorLib/System"> <UI Name="MyMenu"> <Properties> <Choice Name="MenuItems" Choice="$Required" /> </Properties> <Content> <Repeater Layout="VerticalFlow" Source="[MenuItems.Options]"> <Content> <me:MyMenuItem Commands="[MenuItems]" Title="[RepeatedItem!cor:String]" MyIndex="[RepeatedItemIndex]"/> </Content> </Repeater> </Content> </UI> <UI Name="MyMenuItem"> <Properties> <Choice Name="Commands" Choice="$Required" /> <Index Name="MyIndex" Index="$Required"/> <cor:String Name="Title" String="$Required"/> <Color Name="ClickedColor" Color="Red"/> </Properties> <Locals> <ClickHandler Name="ClickHandler"/> </Locals> <Rules> <Default Target="[Input.KeyInteractive]" Value="true" /> <Default Target="[Input.KeyFocusOnMouseEnter]" Value="false" /> <Condition Source="[ClickHandler.Clicking]" SourceValue="true"> <Actions> <Set Target="[CommandLabel.Color]" Value="Red" /> </Actions> </Condition> <Condition Source="[Input.MouseFocus]" SourceValue="true"> <Actions> <Set Target="[CommandLabel.Alpha]" Value="0.75" /> </Actions> </Condition> <Condition Source="[Input.KeyFocus]" SourceValue="true"> <Actions> <Set Target="[Commands.ChosenIndex]" Value="[MyIndex.Value]" /> <Set Target="[CommandLabel.Font]" Value="Segoe UI, 24, Bold" /> <Set Target="[CommandLabel.Alpha]" Value="1" /> </Actions> </Condition> </Rules> <Content> <Text Alpha="0.50" Name = "CommandLabel" Content="[Title]" Color="White" Font="Segoe UI, 20"> </Text> </Content> </UI> </Mcml>

    In order to reuse this control (in the same project or another one), you'll need to :

    • add this file to your project,
    • add it in the resources.resx file
    • add the following line in any mcml file in which you want to use those controls (replacing MyAddin/MyDefaultNamespace )
        <Aggregate Source="resx://MyAddIn/MyDefaultNamespace.Resources/MyMenu" />
    

    In the next few posts, we'll try to make this menu a full fledged control by adding scrolling, commands, accessibility...

  • MCML : First Contact

    For the last view days, I've take some of my spare time to dwell into the new generation of Media Center Extensibility technologies. I've already blogged (and I will do it again with a little more details) about my "Lazy boy server monitoring" tool, but the subject for today is MCML (for Media Center Markup Language).

    MCML doesn't really have much in common with XAML (for WPF or WPF/e), which is sad. Having to learn one more description language and its associated bag of trick is pretty difficult, but the learning curve for MCML is not really long or even hard. In this post, I'll try to explain how to create a simple menu control with a few options (read without any need to scroll).

    After creating a new Media Center Presentation Layer Application, the project have a Default.mcml file that contains a UI element with some content. The UI element is really important in MCML for it's the root container ofevery graphical element you'll build - like a UserControl in asp.net but with the small difference that pages are also an UI element. Clearing the content from this element will bring you with a good starting point for the menu.

    <Mcml 
       xmlns="http://schemas.microsoft.com/2006/mcml"
               xmlns:me="Me">
       <UI Name="MenuPage">
          
       </UI>
    </Mcml>

    We'll start by adding a ColorFill in the MenuPage. This control is similar to a simple Border object in WPF and by default use all the available space in its container but have on advantage : it serves as a layout manager too (meaning that you can arrange its children in different ways : center them, make them flow vertically etc.).

    <Mcml 
       xmlns="http://schemas.microsoft.com/2006/mcml"
        xmlns:me="Me">
       <UI Name="MenuPage">
          <Content>
             <ColorFill Content="LightGray" Layout="Center">
                <Children>
                   
                </Children>
             </ColorFill>
          </Content>
       </UI>
    </Mcml>

    In MCML, there's nothing like a ListView control or any high-end controls. You'll have to go on with a simple Repeater control and a few tricks, but as you will see, there's nothing really complicated.

    MCML is as much a model/view paradigm as XAML is, so to get a list of menu items to display, we'll need three things :

    • a control to represent a menu-item,
    • a control to host those menu items
    • … and a list of menu-items

    For the last requirement, we'll keep it simple : MCML contains a special collection of objects (the Choice class), that handles most of the thing we'll need : a list of object, a property to know which one is currently selected and events when one of its property changes. As nothing can be that simple and easy, there's a catch in using this Class : by default, MCML does not know what a string (for example) is… Like WPF, you'll need to add a xml-namespace to reference the assembly which contains those types.

    As the Choice class is not a visual element, you won't be able to add it to a Content or Children property, but, that's not a big deal : the UI allows you to define local "variables".

    <Mcml 
       xmlns="http://schemas.microsoft.com/2006/mcml"
        xmlns:me="Me"
       xmlns:cor="assembly://MScorLib/System">
       <UI Name="MenuPage">
          <Locals>
             <Choice Name="TheCommands">
                <Options>
                   <cor:String String="Command1" />
                   <cor:String String="Command2" />
                   <cor:String String="Command3" />
                </Options>
             </Choice>
          </Locals>
          <Content>
             <ColorFill Content="LightGray" Layout="Center">
                <Children>
                   <Repeater Layout="VerticalFlow" Source="[TheCommands.Options]">
                      <Content>
                         <Text Content="[RepeatedItem!cor:String]" />
                      </Content>
                   </Repeater>
                </Children>
             </ColorFill>
          </Content>
       </UI>
    </Mcml>

    As you have certainly guessed, the [Something] notation allows to get the value of a variable (the !-notation is used to do some type casting). So we just created a RepeaterItems which displays the options from the local variable TheCommands, and for each value, displays a text. If you take this mcml snippet and run it into MCML Sampler, you should get something like :

    That's not yet amazing, but it will be ;).

    What we need to do next is to replace the simple Text element with something a little bit more complex (what I want to do is to make items become white when selected and red during a mouse-click/enter-key-press). The easiest way to create such a modern - or even futuristic interface - (:p) is to make a user-control (an new UI element) to display the menu item.

    <Mcml 
       xmlns="http://schemas.microsoft.com/2006/mcml"
        xmlns:me="Me"
       xmlns:cor="assembly://MScorLib/System">
       <UI Name="MenuPage">
          ...
       </UI>
       <UI Name="MyMenuItem">
           <Content>
             <Text Content="-not defined-" />
          </Content>
       </UI>
    </Mcml>
    

    We'll need a way to pass the data from the repeater-item to this control, and that's just what the Properties collection is for. These properties are used in the same way that local variables but can be set be the "caller" (parent) control.

    <Mcml 
       xmlns="http://schemas.microsoft.com/2006/mcml"
        xmlns:me="Me"
       xmlns:cor="assembly://MScorLib/System">
       <UI Name="MyMenuItem">
          <Properties>
             <Choice Name="Commands" Choice="$Required" />
             <Index Name="MyIndex" Index="$Required"/>
             <cor:String Name="Title" String="$Required"/>
             <Color Name="ClickedColor" Color="Red"/>
          </Properties>
          <Content>
             <Text Alpha="0.75" 
                Name = "CommandLabel" 
                Content="[Title]" 
                Color="White" 
                Font="Segoe UI, 20">
             </Text>
          </Content>
       </UI>
    </Mcml>
    

    The $Required notation specifies that there is no default value for this property and that the parent should always send a value. Now we just have to change the repeater control :

    ...
    <Repeater Layout="VerticalFlow" Source="[TheCommands.Options]">
       <Content>
          <me:MyMenuItem 
             Title="[RepeatedItem!cor:String]"
             MyIndex="[RepeatedItemIndex]"
             Commands="[TheCommands]"/>
       </Content>
    </Repeater>
    ...

    Okay, we're almost done. In order to get the selected item to be different from the others, we'll need to write some rules. Those rules are almost the equivalent of Triggers in WPF : you can specify a test (of the value of a property/variable) and if this test is true, have MCML apply some changes.

    The rule for displaying the selected item in a different way is :

    <UI Name="MyMenuItem">
       <Rules>
          <Condition Source="[Commands.ChosenIndex]" SourceValue="[MyIndex.Value]">
             <Actions>
                <Set Target="[CommandLabel.Font]" Value="Segoe UI, 24, Bold" />
                <Set Target="[CommandLabel.Alpha]" Value="1" />
             </Actions>
          </Condition>
       </Rules>
       ...
    </UI>

    It's rather simple : we just check that the ChosenIndex (from the Choice object) is equal to the Index of the current item, and if it's true, we set two properties to different values. If it's false, the default formatting will stay. If you run the mcml file now, the first element will stand-out, but you won't be able to change the selected element. For doing that, you'll need to add the following rule (we'll see more details about mouse/keyboard interaction in another post) :

    <UI Name="MyMenuItem">
       <Rules>
          <Default Target="[Input.KeyInteractive]" Value="true" />
          <Condition Source="[Input.KeyFocus]" SourceValue="true">
             <Actions>
                <Set Target="[Commands.ChosenIndex]" Value="[MyIndex.Value]" />
             </Actions>
          </Condition>
          ...
       </Rules>
       ...
    </UI>
    

    It basically tell the run-time that you control is focusable and that you want to change the ChosenIndex (to set it to the item's index) when an item as the focus. You can now run the sample and look at the amazing menu we have done :

    In the next post, we'll look at the way MediaCenter applications can handle keyboard and mouse input.

    Complete source

    <Mcml 
       xmlns="http://schemas.microsoft.com/2006/mcml"
        xmlns:me="Me"
       xmlns:cor="assembly://MScorLib/System">
       <UI Name="MenuPage">
          <Locals>
             <Choice Name="TheCommands">
                <Options>
                   <cor:String String="Command1" />
                   <cor:String String="Command2" />
                   <cor:String String="Command3" />
                </Options>
             </Choice>
          </Locals>
          <Content>
             <ColorFill Content="LightGray" Layout="Center">
                <Children>
                   <Repeater Layout="VerticalFlow" 
    Source="[TheCommands.Options]"> <Content> <me:MyMenuItem Title="[RepeatedItem!cor:String]" MyIndex="[RepeatedItemIndex]" Commands="[TheCommands]"/> </Content> </Repeater> </Children> </ColorFill> </Content> </UI> <UI Name="MyMenuItem"> <Properties> <Choice Name="Commands" Choice="$Required" /> <Index Name="MyIndex" Index="$Required"/> <cor:String Name="Title" String="$Required"/> <Color Name="ClickedColor" Color="Red"/> </Properties> <Rules> <Default Target="[Input.KeyInteractive]"
    Value="true" /> <Condition Source="[Input.KeyFocus]"
    SourceValue="true"> <Actions> <Set Target="[Commands.ChosenIndex]"
    Value="[MyIndex.Value]" /> </Actions> </Condition> <Condition Source="[Commands.ChosenIndex]"
    SourceValue="[MyIndex.Value]"> <Actions> <Set Target="[CommandLabel.Font]"
    Value="Segoe UI, 24, Bold" /> <Set Target="[CommandLabel.Alpha]"
    Value="1" /> </Actions> </Condition> </Rules> <Content> <Text Alpha="0.75" Name = "CommandLabel" Content="[Title]" Color="White" Font="Segoe UI, 20"> </Text> </Content> </UI> </Mcml>