Advanced MessageBox for Windows Phone
This article shows how to create a customised messagebox: asynchronous, custom button, no beep, etc.
Article Metadata
Tested with
Compatibility
Article
Contents |
Introduction
The standard phone message boxes is very easy to use, and is created with just a single line:
Message.Show("Hello!");
But these message boxes aren't easy to customize.
We will learn in this article how to :
- create an asynchronous native message box
- customize buttons
- remove beep
Accessing advanced features through XNA
There is a way to access advanced features of the native messageBox through one back door using XNA.
If you look at the Microsoft.Xna.Framework.GameServices assembly, you will notice that the Guide object has the following methods :
BeginShowMessageBox() method displays a native and asynchronously messagebox with parameters:
- title: Title of the message box
- text: text to be displayed in the message box
- buttons: Legends associated with the buttons on the message box. The maximum number of buttons is two.
- focusButton: 0-based index that defines the button highlighted.
- icon: Type icon in the message box.
- callback: method to call when the asynchronous operation is complete.
- STATE: The unique user-created that identifies this request.
Title and text match mutually the Silverlight parameters caption and messageboxtext of Silverlight.
One difference here: the text can not exceed 256 characters, otherwise an exception is thrown.
Customize buttons
Buttons is here much more advanced than our Silverlight MessageBox, we can specify the button text, for example, we can modify the "ok" and "cancel" text like that :
Microsoft.Xna.Framework.GamerServices.Guide.BeginShowMessageBox(
"Quizz",
"What is your favorite Windows Phone?",
new string[] { "Nokia Lumia 820", "Nokia Lumia 920" },
0,
Microsoft.Xna.Framework.GamerServices.MessageBoxIcon.Alert,
null,
null);
This offers us a lot of new possibilities, but take care to localize your buttons.
How to make synchronous messagebox?
Be able to launch a asynchronous dialog is pretty cool, but in most cases, we expect it to be synchronous. In order to do this, simply retrieve the result of the asynchronous call and wait for its execution.
IAsyncResult result = Microsoft.Xna.Framework.GamerServices.Guide.BeginShowMessageBox(
"Quizz",
"What is your favorite Windows Phone?",
new string[] { "Nokia Lumia 820", "Nokia Lumia 920" },
0,
Microsoft.Xna.Framework.GamerServices.MessageBoxIcon.None,
null,
null);
result.AsyncWaitHandle.WaitOne();
We have now a native, synchronous and customizable message box !
Test result of the dialog
If you want to test the result of your dialog box to know which button has been clicked, simply call the method EndShowMessageBox:
int? choice = Microsoft.Xna.Framework.GamerServices.Guide.EndShowMessageBox(result);
if(choice.HasValue)
{
if(choice.Value==0)
{
//user clicks the first button
}
}
How to remove the messagebox sound?
To remove the beep or the vibration, just change the icon... It's not logical, but that's how it works.
XNA is a common platform between Windows, Xbox and Windows Phone. On windows, we are used to display icons like 'warning', 'alert', etc. on the left of the dialog box but this isn't ideal for mobile screen. The distinction between a normal dialog and a warning/alert dialog box is therefore the sound / vibration. To remove the sound, we just have to set the type to None.
Microsoft.Xna.Framework.GamerServices.Guide.BeginShowMessageBox(
"Quizz",
"What is your favorite Windows Phone?",
new string[] { "Nokia Lumia 820", "Nokia Lumia 920" },
0,
Microsoft.Xna.Framework.GamerServices.MessageBoxIcon.None,
null,
null);
No sound will be launch.
Conclusion
- Add reference Microsoft.Xna.Framework.GameServices (not necessary with Windows Phone 8 project)
- Write the following code:
IAsyncResult result = Microsoft.Xna.Framework.GamerServices.Guide.BeginShowMessageBox(
"Quizz",
"What is your favorite Windows Phone?",
new string[] { "Nokia Lumia 820", "Nokia Lumia 920" },
0,
Microsoft.Xna.Framework.GamerServices.MessageBoxIcon.None,
null,
null);
result.AsyncWaitHandle.WaitOne();
int? choice = Microsoft.Xna.Framework.GamerServices.Guide.EndShowMessageBox(result);
if(choice.HasValue)
{
if(choice.Value==0)
{
//User clicks on the first button
}
}



Contents
R2d2rigo - Wiki competition template
You marked this article as an internal wiki competition, but not the other you published. Please correct the wrong one.r2d2rigo 03:06, 4 December 2012 (EET)
Chintandave er - Thanks and Sub-edited!
Hi, Thanks for this article.
I have sub-edited this article. Also I changed the article title. Also I have corrected contest template as it was for internal employee and I think you are not employee of Nokia or MS. Still let me know if I am wrong.
Have some suggestions for you. Please follow this suggestions and correct those in your articles.
You might want to check out wiki help articles Help:Formatting and Help:Wiki Article Review Checklist.
Regards
Chintan DaveChintandave er 07:21, 4 December 2012 (EET)
Yan -
You can access XNA framework in a WP8 project??yan_ 14:22, 4 December 2012 (EET)
R2d2rigo - XNA in WP8
Yes you can, yan_, but only to some namespaces. You can't make full-XNA games for WP8.r2d2rigo 12:23, 5 December 2012 (EET)
Yan -
Bur is it not deprecated to use XNA in a WP8 application?yan_ 10:59, 6 December 2012 (EET)
R2d2rigo - You can only use some namespaces
Here is the list of allowed ones: http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj207003(v=vs.105).aspxr2d2rigo 16:10, 6 December 2012 (EET)
Yan -
thanksyan_ 17:23, 6 December 2012 (EET)
Hamishwillee - Subedit/Review
Hi Rudy/All
I have given this a basic subedit for wiki style and added a "tip" with the above link on supported XNA namespaces.
Rudy, I like this article. It could be improved by providing references to the classes and guide on the standard message box, and by explaining what the standard box offers (buttons, text, icon) so it is clear what is the delta that your advanced approach offers. As it is now, you state that the new box is better but it isn't clear why. If it were me I'd have a "old" and "new" image next to each other.
It would also be excellent to have a downloadable app where we could try launching both the standard box and your one.
In terms of the competition, this looks like it isn't really a WP8 feature - correct? Which means that it isn't necessarily exactly what we wanted. It is however excellent material for the wiki (expecially with that example code)
Regards
Hamishhamishwillee 06:41, 12 December 2012 (EET)