InputScopes for Windows Phone 7
Posted on August 17, 2010 by Chris Koenig
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
-
http://www.facebook.com/profile.php?id=160000389 Chris Benard
-
http://chriskoenig.net Chris Koenig
-
http://chriskoenig.net Chris Koenig
-
Jim
-
http://chriskoenig.net Chris Koenig





