This stage isn't too hard once you understand the tricks behind it. Let's hook our new MCML file up to C#.
Because of the Model-View seperation that is part of the basic design of Media Center Markup Language projects (see my post on Model-View Seperation), you should carefully plan out exactly what sort of communication you will need between your model (The C# code) and your view (The MCML code).
I'd suggest drawing these interactions, perhaps as use-case diagrams or similar simple drawings to show the sort of information that flows between your C# and your MCML.
In this example, we will want to set the text that appears in the background of our screen - and we want to be able to change it from our code.
First step is to create a C# class we are going to use as our 'main' class for interacting with Media Center. Although you can use several if you want, I've found it much easier to try to stick with a single primary class.
In this case, the class is going to encapsulate everything about our trivia game. It is going to manage questions, answers, keep score etc. But for starters, it is only going to hold one thing - the name of the program as it appears in the top right corner, in our BackgroundText object.
On The C# Side
If you take a quick look at the Stage 1 sample code, you'll notice that I have created a file called TriviaGame.cs. This is where we will create our class, called 'TriviaGame'.
First, set up the class itself...
namespace TriviaCenter
{
public class TriviaGame
{
}
}
OK, so our 'TriviaCenter' namespace now includes a class called TriviaGame.
Now - we are going to need somewhere to keep the title of the program. Why not create a string member to hold this data? (NOTE: Something will come up in the future to make us need to change this - but for now, let's ignore that and just use strings).
String
_GameName;
You may notice that we didn't specify a protection level for this particular variable, which means it defaults to being a private variable. Surely, you'd think, for Media Center to be able to access it, it would need to be declared 'public'?
The truth is that MCML can't read values directly out of members like _GameName. It needs to read information out of properties, not members.
So to access this game name, we are going to need a property. Since we don't anticipate that your MCML file will ever have to change the value, we should only need to provide a get function.
public String GameName
{
get
{
return _GameName;
}
}
There. This defines a property called 'GameName' which can be accessed publically - meaning that Media Center should be able to get to it. This is also the reason I used an underscore (_) key under the name of the member variable _GameName...it avoids confusing the internal object (the member functions) with the properties.
All we need to do now on the C# front is set a default value for GameName. The easiest way to do this is simply to create a constructor and set the value of _GameName to 'triviacenter'.
On The MCML Side
In our MCML, we only need a couple of changes.
The first step is to actually import the assembly containing all of the 'TriviaCenter' objects, allowing us to use our new TriviaGame object. We do this the same way we added the con namespace in the previous version.
xmlns:a
="assembly://TriviaCenter/TriviaCenter"
This line tells Media Center that you will want to use the .NET Assembly located in the 'TriviaCenter' namespace. You will reference all of the members of TriviaCenter by using the prefix a:
Now that we have access to our C# code, let's use it.
First thing to do is make an instance of our new TriviaGame class. Since we are using TriviaGame a lot through the life of this program, we are going to create this object in the very first UI.
You define new instances of classes in the <locals> section. For example, we can creaet a new TriviaGame object with the lines...
<
Locals>
<a:TriviaGame Name="game"/>
</
Locals>
This creates a brand new local variable that can be accessed by this UI (or passed on to other UI's) named 'game'.
To use this new object, find a place where we set the attribute of a tag, such as the Label attribute of the BackgroundText UI. Then, in square brackets (square brackets tell Media Center to substitute a variable instead of reading the data as a literal string) you put in the object path of the data you are interested in.
<
con:BackgroundText Label="[game.GameName]">
So really, there are only two steps - create the local variable, then use the variable in expressions. In this case, the 'GameName' property of the C# object is being loaded by the MCML BackgroundText label.
OK - relatively pain-free, and we can now set a value in our program from the C# code rather than relying on the MCML code. This is an excellent trick if you are planning on making international versions with multiple translations.
Next step is creating something interactive - our first button.
Download the project for this example here.
Posted
Apr 11 2007, 11:53 PM
by
IgnoranceIsBliss