Development, Silverlight, WP7

InputScopes for Windows Phone 7

This entry is part 6 of 8 in the series Windows Phone 7

I have used Shawn Oster’s excellent InputScopes blog post and accompanying demo in the past when talking about Silverlight and the first revisions of the Windows Phone 7 dev tools, but noticed today when I was prepping for a talk that it no longer seems to work right with the Beta bits.

After a little code spelunking, some dusting and even a little vacuuming, I seem to have arrived at a really nice V2 demo.

Concept

The concept is simple – entering text into Silverlight and Windows Phone (via the SIP) is a customizable experience.  The concept of InputScopes was introduced to allow developers to customize how the keyboard would behave for a given input control (e.g. TextBox).  Not all controls support InputScopes, so make sure you read Shawn’s excellent article to get all the details.

Code changes

I only made a few changes, just to keep it in line with how our Best Practices have evolved.

First, I changed the behavior of the EnumHelper class to use LINQ and removed a whole bunch of code:

public static IEnumerable<string> GetNames(Type enumType)
{
    if (!enumType.IsEnum)
    {
        throw new InvalidOperationException("Specified generic parameter must be an enumeration.");
    }
 
    var query = from inputScope in enumType.GetFields(BindingFlags.Public | BindingFlags.Static)
                orderby inputScope.Name
                select inputScope.Name;
 
    return query;
 
}

Next, I cleaned up MainPage.xaml and replaced the code-behind approach with some element-to-element data binding to wire up the selected InputScope.  I also added a counter at the bottom showing how many InputScope objects are available in WP7.

    <Grid x:Name="LayoutRoot"
          Background="Transparent">
 
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="1*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <TextBlock x:Name="PageTitle"
                   Text="input scopes"
                   Grid.Row="0"
                   Style="{StaticResource PhoneTextTitle1Style}" />
        <TextBox x:Name="textBox1"
                 FontSize="32"
                 Grid.Row="1"
                 InputScope="{Binding ElementName=listBox1, Path=SelectedItem}" />
        <ListBox x:Name="listBox1"
                 Margin="10"
                 Grid.Row="2"
                 SelectedItem="Default"
                 FontSize="32" />
        <StackPanel Grid.Row="3"
                    Orientation="Horizontal"
                    HorizontalAlignment="Center">
            <TextBlock FontSize="32"
                       Text="{Binding ElementName=listBox1, Path=Items.Count}" />
            <TextBlock FontSize="32"
                       Text=" InputScopes loaded" />
        </StackPanel>
    </Grid>

That way, the only thing left in my code-behind is the initialization of the ItemsSource property of the ListBox (which I could have changed to use MVVM instead, but hey…)

public partial class MainPage : PhoneApplicationPage
{
    // Constructor
    public MainPage()
    {
        InitializeComponent();
        listBox1.ItemsSource = EnumHelper.GetNames<InputScopeNameValue>();
    }
}

Get the bits!

I’ve posted them on my SkyDrive for easy access.  If you’re in Shreveport tonight, you’ll get to see this LIVE at tonight’s Shreveport .NET User Group meeting, albeit for only about 1 minute Winking smile

Series NavigationCustomizing WP7 Push Notification TilesMVVM Light Messaging

5 thoughts on “InputScopes for Windows Phone 7

  1. I don’t get it. Why do you have that GetNames method when there is an string[] Enum.GetNames(Type enumType) already?

    http://msdn.microsoft.com/en-us/library/system.enum.getnames.aspx

    Couldn’t you just have your helper method (which appears to do only that and sorting) do:

    public static IEnumerable GetNames(Type enumType)
    {
    // Do type checking here if necessary, but I’d bet Enum.GetNames() does it.

    return Enum.GetNames(enumType).OrderBy(x => x);
    }

    Can you post what this looks like? Does this render an invisible listbox that appears when a textbox is selected? Or does this only allow text to be entered that is contained within a listbox? It appears that it’s just a textbox bound to a listbox. I’m unsure of what the InputScope does. At first, when I began reading, I thought it was going to be a constraint like on iPhone where you select a numeric keypad, a URL keypad, or a regular alpha-numeric keypad.

    1. Thank you for your email! I am currently out of the office visiting customers and the local .NET user group in Shreveport, LA. I will have limited access to email until I return to the office on Thursday, August 19th. If this is an emergency, please call my cell phone at 214-385-5616.

      Thanks!
      Chris

    1. Thank you so much for your email! I am currently out of the office speaking at the Windows Phone 7 Developer Launch in Minneapolis and won’t be back in the office until Thursday, October 7th. Until then, responses to email will be delayed until I can dig out of the mountain of mail that will most certainly pile up. If this is an urgent issue, please call my cell phone at 214-385-5616 and I will help you as soon as I can.

      Cheers!
      Chris

Comments are closed.