Platinum Partner
dotnet,mobile,microsoft,windows-phone-7,messagebox

Delivering the message. Using MessageBox in Windows Phone 7.

So how many times did you use a message box when you were developing for a desktop platform? The answer is probably - quite frequently. A message box is a very non-invasive way to tell the user that something needs his attention and maybe ask for a choice of action. Windows Phone 7 also supports message boxes, although in two separate ways. These two ways are tied to two separate frameworks that were designed to be the foundation for the SDK - XNA and Silverlight.

MessageBox in Silverlight

If you decide to develop a Silverlight application for Windows Phone 7, you will have the option of usign MessageBox - the standard class that allows displaying a message to the user. One interesting fact - this MessageBox class is quite limited when it comes to user customization. It is called directly via MessageBox.Show(), just like it was done in a desktop application.

In Silverlight for WP7 there are two overloads:

The first overload will allow the developer to display a simple message with an OK button - as basic as it can get and it is perfect for situations where you don't need the user to make a decision.

So if I would have something like:

MessageBox.Show ("Hello!");

I will get a message that will be displayed like this:

There is no title here and no additional buttons, so this might be a limitation in some situations. Here is when the second overload comes in the game.

It offers a bit more customization by allowing the developer to set the title and actually select, whether there is an OK or OKCancel button set needed.

For example:

MessageBox.Show("The file will be saved here.", "File Save", MessageBoxButton.OKCancel);

This will generate the following output:

It is possible to track the response from the MessageBox via MessageBoxResult:

MessageBoxResult m = MessageBox.Show("The file will be saved here.", "File Save", MessageBoxButton.OKCancel); 

When you want to check the result, you will be promted with an enumeration of possible values and in fact, it is bigger than you expect:

Only OK and Cancel will work if checked against. Other options will simply be ignored, since there is no Yes or No button available. Now, you might think - what about None? This also isn't an option - when the user closes the MessageBox in a non-standard manner (e.g. by pressing the Back button), the result that is automatically passed as a response is MessageBoxResult.Cancel, even if no Cancel button is pressed.

MessageBox in XNA

Yes, you can still use this in Silverlight if you add references to Microsoft.Xna.Framework and Microsoft.Xna. Framework.GamerServices. This is a much more customizable message box compared to what is offered in Silverlight out of the box, mainly because it can have buttons labeled by the developer and sever types of alert sounds.

It is called asynchronously via Guide.BeginShowMessageBox(). This method also has two overloads:

The only difference between the two is that in the second overload you can specify the PlayerIndex, which only accepts PlayerIndex.One both for Windows and Windows Phone, therefore I will use the first overload instead to avoid adding and extra enum value.

Take a look at IEnumerable<string> buttons. What this parameter does is allow the developer to pass a button collection. It is important to mention, however, that the maximum number of allowed buttons here is 2, therefore if a larger collection is passed, an exception will be thrown.

focusButton will be the zero-based index that will define what button will have the default focus when the message is displayed and this value should be strictly tied to the number of items in the collection. 

MessageBoxIcon is in fact not an icon (yeah, I know - the name says so). On Windows Phone 7, it will define the short sound that will accompany the message. And here you have several choices:

Since this is a text article, I cannot really show you how each one of these sounds. All I can say - each of them sounds differently.

Now it is time to work on the callback - it will be triggered when the MessageBox is closed and inside it you have to call Guide.EndShowMessageBox() to get the response.

So let's take a look at a sample call:

 Guide.BeginShowMessageBox("Version of Windows", "Pick a version of Windows.",
new List<string> { "Vista", "Seven" }, 0, MessageBoxIcon.Error,
asyncResult =>
{
int? returned = Guide.EndShowMessageBox(asyncResult);
Debug.WriteLine(returned.ToString());
}, null);

Notice that I am passing a generic list as the button set. Also, inside the callback the returned value is a contained in returned, that is a nullable type - in case the user doesn't select an option, null will be returned therefore you need to handle that.

Once I run this piece of code, I get this:

Remember, that not only this type of MessageBox is more customizable, but it is also called asynchronously, therefore once shown, it will not block the execution of the main application (unlike the MessageBox in Silverlight).

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}