Over a million developers have joined DZone.

Input box? On Windows Phone 7? Indeed.


If you are a seasoned Windows developer, you probably are familiar with the concept of an input box - basically, a modal windows that pops up to prompt the user to enter a single value that is later on returned to the application. In .NET, this concept was implemented as a part of the VB.NET-specific part of the framework and when it came to the point when it should have been used in a C# application, the only choices would be either referencing VB.NET libraries or implementing your own class.

Windows Phone 7 doesn't support any of the regular Windows objects, therefore importing a VB.NET library won't make any sense, simply because it wasn't deisgned to be working on the mobile platform. What many developers (especially those who just started working on their applications) don't know is that there is a built-in implementation of an input box right in the SDK.

It is not that obvious as it might look, especially if the main development is done in Silverlight, since the method I am talking about is included in the XNA part of the SDK. In your Silverlight project, add a reference to Microsoft.Xna.Framework and Microsoft.Xna.Framework.GamerServices.

Now, whenever you need to trigger the input box, you can easily call this line of code:

Guide.BeginShowKeyboardInput(Microsoft.Xna.Framework.PlayerIndex.One, "Title", "Description", "DefaultText", new AsyncCallback(GetString),null);

As you can see, I can set the title, description and the default text for the box. Now, notice one more thing - the operation requires a callback method, and in this case it is called GetString. What I have to do in that method is call EndShowKeyboardInput to signal that the input operation is done and get the actual string:

void GetString (IAsyncResult res)
string test = Guide.EndShowKeyboardInput(res);

So when the page is shown (or whenever you decide to display the box), you should see something like this:

Since I am displaying the result in the Output window, once I click OK, the text is passed to Debug.WriteLine and I can see it displayed:

No text is returned if the user clicks on Cancel, so if the input value is critical, you should check against null and empty returned values.



The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}