Input box? On Windows Phone 7? Indeed.
Join the DZone community and get the full member experience.
Join For FreeIf 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);
Debug.WriteLine(test);
}
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.
Opinions expressed by DZone contributors are their own.
Comments