Monday, February 12, 2007

Thinking and Transferring Data in MCML

OK, I understood the basics of this one when I started, but just to help those newbies out (for instance, there have been a few messages like this on the MediaCenterSandbox), I'll go over the code/view seperation that you find in MCML.

There are two major components of any MCML application. These are the user interface elements (the .MCML files) and the code (the C# program).

In most ocmmon programming models, your code will directly reference the controls For example, you will set the text on the button called 'Button1' to say 'Hello' with the command "Button1.Caption='Hello';" or through messages like "SetDlgItemText(IDC_BUTTON1,'Hello');".

In MCML, the user interface and your code are kept seperate. You never directly reference any controls or user interface elements from your code.

Instead, MCML has access to the properties of your objects. So basically you choose what properties you want to show on your interface and hand them to your MCML page as properties.

Your MCML page is where your object is created. You can then access all of the public properties of the object and use them in your MCML file.


An example of passing a value from C# to MCML...


So in C#, you have a class like this

public class MyObject
{
//The underlying data

protected string _MyStringValue;

//The property to read in MCML
public String MyStringValue
{
get
{
return _MyStringValue;
}
}
}


And then if you want an MCML text object that uses the value...


<UI Name="MyObject">

<Content>
<Text Name="MyData" Color="White"/>
</Content>

<Locals>
<a:MyStringValue Name="Val"/>
</Locals>
<Rules>
<Binding Source="[Val.MyStringValue]" Target=[MyData.Content]"/>
</Rules>
</UI>


This assumes you are importing your C#/.NET assembly with the namespace 'a' (this is the default when you create an MCML project in Visual Studio, so you shouldn't have to add this line to the first tag of your document).

2 comments:

Anonymous said...

Appreciations.
I am also working on MCML and indeed your blog is very helpful for new comers. But I have some concern to share with you if you can give some suggestions please give me.
Is it proper way to use Application object each time in different MCML pages. Is there any way to use a single Application object in multiple MCML pages.

Ignorance said...

Please don't use this blog - it's very old and is not updated anymore. Please use http://developer.thedigitallifestyle.com instead.

But to answer your question, yes - you can easily pass the one application object through your app.

When you call 'GoToPage' (in C#) or 'Navigate' (in MCML), which makes Media Center open new MCML pages, you can pass your application object as a parameter.

If you install the latest SDK, you'll find that the example program created for you actually creates the Application object FIRST, then passes it as a parameter to all of the MCML pages.