Tuesday, February 13, 2007

Installing Your Program - Part 2 - Installing Your Files

Step 2 of the install process is nice and easy - no command-prompt needed :)

Create a new project (add it to the existing solution) in Visual Studio. Under 'Other Projects', you'll find "Setup and Deployment". This will allow you to create an installer.

Give your program a name like <program name>Setup - try to steer clear of generic names like 'setup', simply because it's very easy to get your 'setup.msi' confused with hundreds of others with exactly the same name.

Now your new project should appear in the Solution Explorer. Click it, and go to the 'View' menu. Under 'View', you should now have the 'Editor' submenu. This allows you to choose to edit the various parts of your installation - the files, registry settings etc.

Choose 'File System', if it isn't selected already.

The first entry will read 'File System on Target Machine'. Right-click this line and a popup menu will appear. Choose 'Add Special Folder|Global Assembly Cache Folder' to add the GAC to your installer.

Right click on the new folder that will appear and choose 'Add|Project Output...' from the popup menu. You can then 'Primary Output' from the list of different types of file you can include from your project.

This will add your .NET classes into the Global Assembly Cache, as well as get a reference to a number of extra DLL's and modules that your application will require. If you check the solution explorer, you'll notice that Microsoft.MediaCenter.dll and a couple of it's friends will have appeared.

We don't need to install these, although they ARE required for your program to work correctly. To avoid installing these (which would be illegal), we right click on the files and choose 'Exclude'.

There! Your .NET files are now installing. The next thing we need to do is tell Media Center where our program is, what it does and what it's called. That's covered in the next post...

Installing Your Program - Part 1 - Signing Your Assembly.

This is the first part of my little review on writing an installer to get your MCML program into Media Center.

The code part of your Media Center program (usually written in C#) is installed into the Global Assembly Cache (GAC) of Windows. The GAC is used to store .NET assemblies that are shared across multiple programs. Your basic Windows forms classes etc. all live in the GAC.

Before any program can be added to the cache, it must be strongly named. A strongly named assembly has a public and private key associated with it, to ensure that it is actually the original class and not a modified or doctored version.

To generate this key, you will need to use the 'SN' command that ships with Visual Studio. You will find it in <Visual Studio Install Directory>/SDK/v2.0/BIN

Use 'cmd' (the command-line interpreter) or even better, open the command prompt from the start menu, under 'Programs|Visual Studio 2005' so you won't need to bother with your path information.

Type 'sn -k <filename>.snk' to generate a new key file.

Then in Visual Studio, right click on your project in the project view and choose 'Properties' from the drop-down menu that will appear. Under 'signing' you will have the option of choosing a key file. Choose the one you created with the SN utility, and rebuild your project.

There! You have a signed assembly, ready for installation into the GAC.

Monday, February 12, 2007

Yougle Now Installing

My next blog entry will be on writing installers.

Just to prove that I'm not COMPLETELY full of it, here's an image from my current MCML project.



Of course there's a lot of work to be done, the interface has to be prettied up a lot - but at least you know I'm not just making all of this up :)

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).