×
Namespaces

Variants
Actions
Revision as of 21:56, 3 December 2012 by rudyhuyn (Talk | contribs)

C++ support from Windows Phone 8

From Nokia Developer Wiki
Jump to: navigation, search

Delete instructional text in italic

This article explains how to ... Replace the abstract text with a short paragraph (or sentence) describing what the topic covers.

Enter article metadata as described below. Note that this template can be placed anywhere in the article. Do not remove parameters that you do not use

WP Metro Icon DirectX.png
WP Metro Icon WP8.png
Article Metadata
Compatibility
Platform(s):
Windows Phone 8
Article
Created: (16 Nov 2012)
Last edited: rudyhuyn (03 Dec 2012)

Contents

Introduction

It's 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 :



 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 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(
"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();

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)
{
//user clicks the first button
}
}

Summary

  • Add reference Microsoft.Xna.Framework.GameServices
  • 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
}
}

How to remove 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 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);
1649 page views in the last 30 days.
×