MyMessageBox for Phone and Store apps

Tue, February 5, 2013, 02:41 AM under MobileAndEmbedded

I am sharing a class I use for both my Windows Phone 8 and Windows Store app projects.

Background and my requirements

  1. For my Windows Phone 7 projects two years ago I wrote an improved custom MessageBox class that preserves the well-known MessageBox interface while offering several advantages. I documented those and shared it for Windows Phone 7 here: Guide.BeginShowMessageBox wrapper.
  2. Aside: With Windows Phone 8 we can now use the async/await feature out of the box without taking a dependency on additional/separate pre-release software.
  3. As I try to share code between my existing Windows Phone 8 projects and my new Windows Store app projects, I wanted to preserve the calling code, so I decided to wrap the WinRT MessageDialog class in a custom class to present the same MessageBox interface to my codebase.
  4. BUT. The MessageDialog class has to be called with the await keyword preceding it (which as we know is viral) which means all my calling code will also have to use await. Which in turn means that I have to change my MessageBox wrapper to present the same interface to the shared codebase and be callable with await… for both Windows Phone projects and Windows Store app projects.

Solution

The solution is what the requirements above outlined: a single code file with a MessageBox class that you can drop in your project, regardless of whether it targets Windows Phone 8, or Windows 8 Store apps or both. Just call any of its static Show functions using await and dependent on the overload check the return type to see which button the user chose.

// example from http://www.danielmoth.com/Blog/GuideBeginShowMessageBox-Wrapper.aspx
if (await MyMessageBox.Show("my message", "my caption", "ok, got it", "that sucks")
== MyMessageBoxResult.Button1)
{
     // Do something
     Debug.WriteLine("OK");
}

The class can be downloaded from the bottom of my older blog post.


    
Comments are closed.