Author Archive

Creating custom Transformers

Transformers come in handy when you want to convert a value in a rule. Media Center has some build-in transformers that can do some math and string-operations, among others. You can also build your own custom transformers, in this post I will try to explain how.

First of all you need to create a class that implements the ITransformer interface. The example below shows a custom Transformer that returns true when the given integer-value is odd and otherwise returns false:

public class IntIsOddTranformer : ITransformer
{
#region ITransformer Members

public object Transform(object value)
{
int i = (int)value;
return ((i & 1) == 1);
}

#endregion
}

In order to use the transformer in MCML, you need to create a local that represents the transformer, like this:

<Locals>
<a:IntIsOddTranformer Name=”IntIsOddTranformer”/>
</Locals>

Now you’re ready to create the rule:

<Rules>
<Binding Target=”[SomeBoolean]” Source=”[SomeInt32]” Transformer=”[IntIsOddTranformer]”/>
</Rules>

This rule will set the target to true when the source odd and to false when the source is even.

More complex transformers may need extra information to perform their task, you can provide this information by adding properties to the class. These properties can then be set where the Local for the transformer is created or through a binding.

Good luck!

March 26, 2008 at 8:48 pm Leave a comment

Something odd about MCML rules, in my opinion at least..

To me the code below makes perfect sense; execute the binding when the conditions are met, thus executing the inner conditions before the outer binding.

<Binding Target=”[Text.Content]” Source=”[Application.MyObject!a:MyDerivedObject.Property]”>
<Conditions>
<IsType Source=”[Application.MyObject]” Type=”a:MyDerivedObject”/>
<IsValid Source=”[Application.MyObject!a:MyDerivedObject.Property]”/>
</Conditions>
</Binding>

But it doen’t work this way in MCML, when the property is invalid, an exception is raised because the binding could not be made. So the parser tries to execute the binding-rule first, instead of the inner conditions.

The proper way to define this rule is:

<Rule>
<Conditions>
<IsType Source=”[Application.MyObject]” Type=”a:MyDerivedObject”/>
<IsValid Source=”[Application.MyObject!a:MyDerivedObject.Property]”/>
<IsModified Source=”[Application.MyObject!a:MyDerivedObject.Property]”/>
</Conditions>
<Actions>
<Set Target=”[Text.Content]” Value=”[Application.MyObject!a:MyDerivedObject.Property]”>
</Actions>
</Rule>

March 10, 2008 at 10:32 am Leave a comment

How to make the focus-rectangle slide from one item to the next in a list

When you move the input-focus from one item to the next in a Media Center list, the focus-rectangle slides along in a really neat way. In this post I will try to explain how you can reproduce this behaviour in your own application.

The key to solving this issue is using the ‘Focus’-anchor in a Form- or AnchorLayout.

First of all, create a Panel that contains both a Scroller, containing the list, and a container for the focus-image. Place the focus-image code above the Scroller code, making the focus-image appear on top of the scroller-content.
Then apply the appropriate Layout and LayoutInputs as shown in de code below. Note the bold line, this is the code that essentially makes the focus-image move along with the input-focus. The animation takes care of the slide-effect.

<Panel Name=”GalleryContainer” Layout=”Form”>
<Children>
<Panel Name=”ButtonFocusContainer”>
<Layout>
<AnchorLayout SizeToHorizontalChildren=”true” SizeToVerticalChildren=”true”/>
</Layout>
<LayoutInput>
<FormLayoutInput Left=”Focus,0″ Top=”Focus,0″/>
</LayoutInput>
<Children>
<Graphic Content=”image://styles:ButtonFocus”/>
</Children>
<Animations>
<Animation Type=”Move”>
<Keyframes>
<PositionKeyframe Time=”0″ RelativeTo=”Current” Interpolation=”Exp”/>
<PositionKeyframe Time=”0.2″ RelativeTo=”Final”/>
</Keyframes>
</Animation>
</Animations>
</Panel>
<Scroller Name=”GalleryScroller” >
<LayoutInput>
<FormLayoutInput Top=”Parent,0″ Left=”Parent,0″/>
</LayoutInput>
<Children>
<Repeater Name=”GalleryRepeater”>
</Children>
</Scroller>
</Children>
</Panel>

March 6, 2008 at 9:50 am Leave a comment

How to attach the debugger at application startup

You may have already found the solution to this in the SDK, if not; search the SDK help for “Debugging” and check the first hit.

I have created a little piece of code that makes modifying the registry key a lot easier:
(A batch file could also have done the job, but remember, I’m a software developer)

Create a WinForm app, include the class below and link its Enabled-property to the Checked-property of a Checkbox.

using System;
using Microsoft.Win32;

namespace MediaCenterDebugRegSetter
{
public static class MediaCenterAddInLaunchDebugging
{
public static bool Enabled
{
get
{
RegistryKey key = GetRegistryKey(false);
if (key != null)
{
object value = key.GetValue(“EnableAddInLaunchDebugging”);
key.Close();
return ((value != null) && ((int)value == 1));
}
return false;
}
set
{
RegistryKey key = GetRegistryKey(true);
if (key != null)
{

key.SetValue(“EnableAddInLaunchDebugging”, (value == true) ? 1 : 0, RegistryValueKind.DWord);
key.Flush();
key.Close();
}
}
}

private static RegistryKey GetRegistryKey(bool writable)
{
try
{
return Registry.CurrentUser.OpenSubKey(@”SoftwareMicrosoftWindowsCurrentVersionMedia CenterSettingsExtensibility”, writable);
}
catch
{
return null;
}
}
}
}

PS: Media Center must be restarted for the new setting to take effect.

March 4, 2008 at 3:09 pm 3 comments

Understanding Layouts and LayoutInputs

Creating a layout can be somewhat cumbersome at first, in this post I will try to explain how to create a common layout.

My experience is that FormLayouts and AnchorLayouts are the most useful of all types of layouts, the two act almost similar and the only difference is that the former always fills to the size of its parent, the latter sizes to the combined size of its children.
By applying a FormLayout to the outermost Panel or ColorFill in a UI, as the SDK recommends, you can take full control of positioning viewitems on the screen as children of this Panel or ColorFill.
To align these children, FormLayoutInputs should be applied to them, for example one like this:

<FormLayoutInput Top=”Parent,0,50″ Right=”Parent,1,-30″/>

This is what it actually means:

The right border of the viewitem is anchored at 100% of width of the parent, minus 30px. The first parameter of the Right-attribute is the reference for defining the anchor, the second parameter is the offset that should be applied as a fraction of the width of the parent, the third is an additional offset in pixels.

The same logic can be applied to the Top-attribute, except that the offsets relate to the height and the vertical position, so the top of the viewitem is anchored 50px below the top of the parent.

The reference can either be the Parent viewitem, the name of a sibling viewitem or Focus. The Focus-reference anchors a viewitem to whichever sibling that has input-focus.

Good luck!

March 4, 2008 at 11:38 am 1 comment

Welcome to my blog!

I have recently learned a lot about developing applications for Windows Vista Media Center, this blog is dedicated to sharing my newly acquired knowledge.

When you’re just getting started, make sure you take a look at the samples included in the SDK and the Media Center Sandbox forum.

February 27, 2008 at 12:33 pm 2 comments


Categories

Feeds