Asynchronous MessageBox

Asynchronous MessageBox

It is sometimes useful to launch a messagebox asynchronously.

By default, all silverlight messagebox are synchronous but… there is a solution.

solution

One might consider reimplementing the messagebox, it is a possibility, but we will see today that it is possible to access advanced features of MessageBox through one back door, thanks to our friend XNA!

If you look at the Microsoft.Xna.Framework.GameServices assembly, you will notice that the Guide object has methods quite interesting:

BeginShowMessageBox !

This 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 parameters “caption” and “messageboxtext” of Silverlight.

One difference here: the text can not exceed 256 characters, otherwise an exception is thrown.

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 :

*Le example

Microsoft.Xna.Framework.GamerServices.Guide.BeginShowMessageBox(
"Aimez vous Fuse ?",
"Avez vous acheté la super application Fuse ?",
new string[] { "évidemment !", "c'est quoi ?" },
0,
Microsoft.Xna.Framework.GamerServices.MessageBoxIcon.Alert,
null,
null);

This offers us a lot of new possibilities!

But be careful to localize your buttons!

How to make synchronous messagebox again?

Able to have a dialog asynchronous’s pretty cool, but in most cases, we expect it to be synchronous. To do this simply to retrieve the result of the asynchronous call and wait for its execution.

IAsyncResult result = Microsoft.Xna.Framework.GamerServices.Guide.BeginShowMessageBox(
"Do you like  Fuse ?",
"And do you buy it ?",
new string[] { "of course !", "what's Fuse ?" },
0,
Microsoft.Xna.Framework.GamerServices.MessageBoxIcon.None,
null,
null);

result.AsyncWaitHandle.WaitOne();

and here we have a native, synchronous and customizable message box !

Test result of the dialog

If you want to test the result of your dialog box (which button was clicked), simply call the method EndShowMessageBox :

int? choice = Microsoft.Xna.Framework.GamerServices.Guide.EndShowMessageBox(result);
if(choice.HasValue)
{
if(choice.Value==0)
{
//usezr clicks the first button
}
}

Summary

  • Add reference Microsoft.Xna.Framework.GameServices
  • Write the following code:

 

 

IAsyncResult result = Microsoft.Xna.Framework.GamerServices.Guide.BeginShowMessageBox(
"Do you like Fuse ?",
"and do you buy it ?",
new string[] { "of course !", "what is fuse ?" },
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)
{
//l'utilisateur a cliqué sur le premier bouton
}
}

Bonus

How to disable the messagebox sound  ?

 

To remove the beep or the vibration, just change the icon … Ok I know this is not great logic, but that’s how. In fact it should be understood that XNA is a common platform between Windows, Xbox and Windows Phone. In the windows world, we are used to display icons ‘warning’, ‘alert’, etc. .. left of the dialog box, which is not super top for a mobile display or width of the screen is already binding. The distinction between a normal dialog and a dialog box warning / alert, is therefore the sound / vibration. To remove the sound, we just have not typify the messagebox:

 

 

Microsoft.Xna.Framework.GamerServices.Guide.BeginShowMessageBox(
"Do you like Fuse ?",
"and do you buy it ?",
new string[] { "of course !", "what is fuse ?" },
0,
Microsoft.Xna.Framework.GamerServices.MessageBoxIcon.None,
null,
null);

Voilà !!!

Comments are closed.