Posts Tagged ‘dialog’

Code based dialogs in media center

Wednesday, March 10th, 2010

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.

Simpler popup dialog

Sunday, April 19th, 2009

In a previous post on adding a version checker to media center, I gave an example of how to write a popup dialog box for media center. There is also a simpler version of the Dialog if you don’t need to know anything about the response, e.g. if you are simply showing an information message. This simpler version doesn’t require a callback to be defined, or any complex button definitions. It just needs a title, some text, a button name, a timeout and whether or not it is modal. Here’s an example;

      <Condition Source="[Preferences.TwitDialog]" SourceValue="true" ConditionOp="ChangedTo">
        <Actions>
          <Set Target="[Preferences.TwitDialog]" Value="false"/>
          <Invoke Target="[AddInHost.MediaCenterEnvironment.Dialog]"
                  caption="Authorize Twitter"
                  text="Please authorize twitter."
                  buttons="Ok"
                  timeout="15"
                  isModal="true"/>

        </Actions>
      </Condition>

The buttons can only be ones from the list described in the documentation. A modal window requires either the user or a timeout to close it before any other actions can be done, whereas a modeless window allows the user to continue with other things in the meanwhile.

The dialog is fired in this example by a property which is set in the code to true, and then set back to false when it is read. The condition only fires when the value is set to true in the first place by using the ConditionOp.