Guide.BeginShowMessageBox wrapper

Thu, December 30, 2010, 12:38 AM under MobileAndEmbedded

imageWhile coding for Windows Phone 7 using Silverlight, I was really disappointed with the built-in MessageBox class, so I found an alternative. My disappointment was the fact that:

  1. Display of the messagebox causes the phone to vibrate (!)
  2. Display of the messagebox causes the phone to make an annoying sound.
  3. You can only have "ok" and "cancel" buttons (no other button captions).

I was using the messagebox something like this:

      // Produces unwanted sound and vibration. 
      // ...plus no customization of button captions.
      if (MessageBox.Show("my message", "my caption", MessageBoxButton.OKCancel)
              == MessageBoxResult.OK)
      {
        // Do something
        Debug.WriteLine("OK");
      }

…and wanted to make minimal changes throughout my code to change it to this:

      // no sound or vibration
      // ...plus bonus of customizing button captions
      if (MyMessageBox.Show("my message", "my caption", "ok, got it", "that sucks")
              == MyMessageBoxResult.Button1)
      {
        // Do something
        Debug.WriteLine("OK");
      }

It turns out there is a much more powerful class in the XNA framework that delivered on my requirements (and offers even more features that I didn't need like choice of sounds and not blocking the caller): Guide.BeginShowMessageBox. You can use it simply by adding an assembly reference to Microsoft.Xna.Framework.GamerServices.

I wrote a little wrapper for my needs and you can find it here (ready to enhance with your needs): MyMessageBox.cs.old.txt.

UPDATE 2013: If you don’t mind using await in front of the call to MyMessageBox.Show, I have an updated class that works for both Windows Phone 8 and for Windows 8 Store apps here: MyMessageBox.cs.txt.