Creating custom Transformers

March 26, 2008

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!


Entry Filed under: MCML, Managed code, Windows Media Center. .

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Recent Posts

Categories

Archives

Blogroll

Feeds