AddThis Social Bookmark Button

Print

Building .NET Add-Ins for Windows Media Center Edition
Pages: 1, 2

Building and Registering your Add-In

Now that we've got the code written, we'll need to compile it. We'll also need to register the compiled DLL with Media Center so it knows that it exists. We're going to use the compiler from the .NET directory on the Media Center to compile our code. Create another text file and paste the following text into it:



Echo Off
Echo Building Add-in...
c:\WINDOWS\Microsoft.NET\Framework\v1.0.3705\csc.exe /target:library /reference:C:\WINDOWS\eHome\Microsoft.MediaCenter.dll HelloWorld.cs AssemblyInfo.cs
Echo copying file to eHome directory...
copy HelloWorld.dll C:\WINDOWS\eHome\HelloWorld.dll
Echo File Copied to eHome directory.
pause

Save the file to the same folder you just created, and name it make.cmd. When you double-click on this file, it will build the code and copy the resulting DLL to the directory that contains Media Center applications.

Registering the Class as a Background Add-In

Now that we've built the DLL and copied it to the correct folder, we'll need to register it with Media Center. We do this by creating an XML file containing the registration settings that Media Center needs and then calling RegisterMCEApp.exe (included with Media Center).

Note: The best way to register an add-in is to create a Windows Installer file. See the online SDK documentation for more details.

Create a new text file in the folder that you created above. Paste the following XML into the new text file:

<application title="Sample Add-In" 
    id="{CEA2344E-31EF-4136-A865-37373AD3CED3}">
  <entrypoint 
      id="{CEA2344E-31EF-4136-A865-37373AD3CED3}"
      addin="Sample.HelloWorld, HelloWorld"
      title="Hello, World"
      description=".NET Add-in Sample">
    <category category="Background" />
  </entrypoint>
</application>

Save the file as HelloWorld.mcl.

Open the make.cmd file from above and add the following text at the end of the file:

Echo Registering application with Media Center...
C:\WINDOWS\eHome\RegisterMCEApp.exe "c:\SAMPLE_FOLDER_PATH\HelloWorld.mcl"
pause

Change the SAMPLE_FOLDER_PATH to the correct path, save the file, and then double-click on it to compile, copy, and register it with Media Center.

Start Media Center by pressing the Start button on your remote control. You should see the dialog pop up with the message "Hello, World" (Figure 1). Congratulations; you've just created your first add-in. Close the Media Center application to continue.

Figure 1
Figure 1: Your first add-in

Notice how our add-in executed as soon as we started Media Center. It did this because we set it up as a background application in the .mcl file above by adding the following category information:

<category category="Background"/>

Registering a Class as an On-Demand Add-In

How would we make an on-demand add-in? It's simple. All we need to do is tell Media Center how we want it to be executed. We do this by using one of the 16categories defined in the Media Center SDK:

  • Internet Radio
  • More Programs
  • More With This\Audio\Album
  • More With This\Audio\Artist
  • More With This\Audio\Playlist
  • More With This\Audio\Song
  • More With This\Audio\Genre
  • More With This\DVD
  • More With This\Picture
  • More With This\Video
  • New For Me
  • Services\Audio
  • Services\Movies
  • Services\Pictures
  • Services\Radio
  • Services\TV

For our sample, we'll register our add-in to handle requests from More With This\Audio\Album. This means that our add-in will run when we press the More Information button on the Media Center remote control and select More from the resulting menu. Let's add some code to our add-in that will display the album artist in a dialog box when we do this. Open the HelloWorld.cs file and replace the original dialog code in the IAddInEntryPoint.Launch method with the following line of code:

mcHost.HostControl.Dialog("Artist: " + 
	  mcHost.HostControl.MediaContext["AlbumArtist"], 
    "Artist Info", 1, 10, false);

Open the HelloWorld.mcl file in a text editor and replace the <category> element with the following XML text:

<category category="More With This\Audio\Album"/>

Save the file. Before we build and register our add-in, let's ensure that our add-in is unregistered before it is built. This will ensure a clean registration each time we build it. Open the make.cmd file in a text editor and add the following text to the top of the file (just after the Echo Off line):

Echo Unregistering application from Media Center
C:\WINDOWS\eHome\RegisterMCEApp.exe /u "c:\SAMPLE_FOLDER_PATH\HelloWorld.mcl"

Save make.cmd, and then run it. Once your add-in has been built and registered, start up Media Center. Navigate to My Music and click on the OK button. Once the Albums list has loaded, navigate to one of the albums in your collection then press the More button on your Media Center remote control (Figure 2).

Figure 2
Figure 2. Using your new add-in

Select More, and click on OK (Figure 3).

Figure 3
Figure 3. Using your new add-in

Select Hello, World from the list and click on OK. You should see a dialog that shows the name of the album (Figure 4).

Figure 1
Figure 4. Using your new add-in

You have now created an on-demand add-in that interacts with the Media Center context.

Conclusion

This article took you through a very basic scenario to introduce you to the steps involved with creating your own add-ins for Media Center using .NET. There is an entire world of possibility out there for you to explore from within Media Center. The following resources will help you in your quest to create that next killer application for Windows XP Media Center Edition 2005:

Michael Earls is a Microsoft technology solutions consultant working for Magenic Technologies.


Return to ONDotnet.com.