Showing an input box on Windows Phone 7

0.00 avg. rating (0% score) - 0 votes

In a Windows Phone 7 application, how can you quickly show a dialog asking the user to enter an input string without creating a new XAML page? The easiest method would be to call Guide.BeginShowKeyboardInput from the Microsoft.Xna.Framework.GamerServices namespace, which offers a similar user experience to the .NET framework’s InputBox method, available in the .NET framework as part of the Microsoft.VisualBasic namespace.

First you will need to add references to Microsoft.Xna.Framework and Microsoft.Xna.Framework.GamerServices to your project. After that, use the following code:

Guide.BeginShowKeyboardInput(Microsoft.Xna.Framework.PlayerIndex.One, "Title goes here", "Description goes here", "default text", new AsyncCallback(Value_Entered), null);

The prompt will be shown:


Handle the response by:

void Value_Entered(IAsyncResult res)
     string value = Guide.EndShowKeyboardInput(res);

Notice that the returned value will be an empty string when the CANCEL button is clicked or when the user does not enter any value and simply clicks the OK button. Another restriction is that the dialog will automatically be closed if the application enters the background (e.g. when the Windows key is pressed) and will not be redisplayed when the application is subsequently activated. This makes the use of BeginShowKeyboardInput unsuitable for scenarios when the user needs to leave the application temporarily before the code can be entered, for example when the dialog asks for a verification code sent to the user’s phone number as part of the mobile number verification process and the user needs to leave the application to open the Messaging application to check for new messages.

In those scenarios, a more suitable approach is to use the InputPrompt class available is the coding4fun controls toolkit for Windows Phone. To install this, from Visual Studio Project menu, select Manage NuGet Packages and install the Coding4fun Toolkit – Controls package:

After the installation, restart Visual Studio and you will be able to use the InputPrompt class. If you encounter the error “You are trying to install this package into a project that targets … but the package does not contain any assembly references that are compatible with that framework.”, please reinstall the latest version of NuGet using the Tools>Extensions and Updates menu in Visual Studio.

You can show the input message by using the following code:

InputPrompt input = new InputPrompt();
input.InputScope = new InputScope { Names = { new InputScopeName() { NameValue = InputScopeNameValue.Number} } };
input.Completed += input_Completed;
input.Title = "Basic Input";
input.Message = "I'm a basic input prompt"; 
input.Value = "";  
input.MessageTextWrapping = TextWrapping.Wrap;
input.IsCancelVisible = true;

The dialog will be shown:

Handle the response by:

void input_Completed(object sender, PopUpEventArgs<string, PopUpResult> e)
    string value = e.Result;

With this approach, the dialog will still remain open when the user leaves the application and comes back, which is better than using the BeginShowKeyboardInput method. Take note that this will only work if the user returns to the application by holding the BACK key and selecting the application icon. If the user clicks on the application icon in the Start menu, due to a restriction in the Windows Phone 7 SDK, the application will be restarted from the beginning and the input prompt will no longer be shown. On Windows Phone 8, the user can also return to the input prompt using the Start menu icon if the following modification is added to the task entry in the WMAppManifest.xml file to enable fast app resume:

<DefaultTask Name="_default" NavigationPage="MainPage.xaml" ActivationPolicy="Resume"/>

For more information on the customizations that can be done on the InputPrompt class, refer to this.

0.00 avg. rating (0% score) - 0 votes


A tough developer who likes to work on just about anything, from software development to electronics, and share his knowledge with the rest of the world.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>