Code based dialogs in media center

I’ve written a few posts on how to implement dialog boxes in media center (Adding a version checker and Simpler popup dialog). They are both great examples of how to trigger a dialog box in mcml. Suppose though you want to do it all from within code, not within mcml. Why would you want to do that? Well, for one it means not having any AddIn calls in your mcml, and AddIn calls mean you can’t use the mcml preview tool – which significantly hampers development. For this example, I’m going to repeat the Adding a version checker example, but all in code.

So, to trigger a dialog from your code, the first thing you need to do is to build some buttons;

ArrayListDataSet dialogButtons = new ArrayListDataSet();
dialogButtons.Add("Download Now");
dialogButtons.Add("Remind me later");
dialogButtons.Add("Ignore");

This adds 3 buttons to the dialog. Obviously add as many or as few as you like. The next thing you need to do is to add the call to open the dialog;

String dialogText="There is a new version available";
String dialogTitle="New Version";
Microsoft.MediaCenter.Hosting.AddInHost.Current
.MediaCenterEnvironment.Dialog(dialogText
, "dialogTitle, dialogButtons, 30, true, null
, new Microsoft.MediaCenter.DialogClosedCallback(versionCallback));

This will open a dialog as a modal dialog with a timeout of 30 seconds. When it is closed, the method?versionCallback will be called, so now we need to write that method;

public void versionCallback(Microsoft.MediaCenter.DialogResult result)
{
    if (result.ToString() == "100")
    {
        InstallNewVersion();
    }
    if (result.ToString() == "101")
    {
        RemindLater();
    }
    if (result.ToString() == "102")
    {
        IgnoreNewVersion();
    }
}

And obviously fill in the appropriate actions for each method.

Finally, you may want to block this dialog from triggering when you are in the debugger (since it won’t work), so put an if statement round it to stop it running in debug mode;

#if(!DEBUG)
   String dialogText="There is a new version available";
   String dialogTitle="New Version";
   Microsoft.MediaCenter.Hosting.AddInHost.Current
.MediaCenterEnvironment.Dialog(dialogText
  , "dialogTitle, dialogButtons, 30, true, null
  , new Microsoft.MediaCenter.DialogClosedCallback(versionCallback));
#endif

And that’s it – an mcml dialog operated entirely from your code.

Tags: , ,

Leave a Reply