Wednesday, March 24, 2010

Windows Phone 7 Development: Using InputScopes

 

I went to Mix2010 this year and attended Mike Harsh’s talk on Building Windows Phone Applications with Silverlight (Part 1). After the session, I wanted to play around with InputScopes a little bit more, so I decided to try replicate the sample application he showed during the session. It is a really simple application. I figured I would post it since the presentation didn’t cover all of the implementation details.

There is a textbox where the user can type text and a listbox which contains all the available InputScopes. When the user selects an InputScope, it is applied to the textbox. This controls what the virtual keyboard looks like when the user is typing in. For instance, if the user selects the TelephoneNumber InputScope, the keyboard will look like this

image

But if he or she selects the InputScope the keyboard will look like this

image 

So, first to the xaml. This part couldn’t be simpler. Inside the content grid, I added a textbox and a listbox. I also changed the title lable to InpuScopes. That’s it for the xaml

<TextBox x:Name="txtInput" Height="50" VerticalAlignment="Top" />           
<
ListBox Height="580" x:Name="lstScope" HorizontalAlignment="Stretch" Margin="0,66,0,0" VerticalAlignment="Top" Width="460" />


And this is what it looks like. In this screenshot, the listbox is already populated, but we’ll take care of that in a minute in the code behind.



image



In the class constructor I added the bottom two lines. The first one calls a GetNames method which basically returns a list of all the names of the given Enum. The second line simply assigns an event handler to the SelectionChanged event of the listbox. This could have been done in xaml, but I arbitrarily chose to do it in code.



public MainPage()
{
InitializeComponent();

SupportedOrientations = SupportedPageOrientation.Portrait | SupportedPageOrientation.Landscape;

lstScope.ItemsSource = this.GetNames(typeof(InputScopeNameValue));
lstScope.SelectionChanged += new SelectionChangedEventHandler(lstScope_SelectionChanged);
}


Here is the implementation of the GetNames method. This will take any enumeration type and return a list of strings with all the values.


private  IEnumerable<string> GetNames(Type enumType)
{
if (!enumType.IsEnum)
{
throw new InvalidOperationException("Not an enum type");
}

List<string> nameList = new List<String>();
FieldInfo[] fiArray = enumType.GetFields();
foreach(FieldInfo fi in fiArray)
{
if (fi.FieldType.IsEnum)
{
nameList.Add(fi.Name);
}
}

return nameList;
}


Finally, I implemented the SelectionChanged event handler for the listbox. I basically get the appropriate enum value from the selected string and call the SetInputScope method which essentially sets the input scope for the textbox. That’s it. 


void lstScope_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
InputScopeNameValue val;

if (Enum.IsDefined(typeof(InputScopeNameValue), lstScope.SelectedItem))
{
val = (InputScopeNameValue)Enum.Parse(typeof(InputScopeNameValue), lstScope.SelectedItem.ToString(), true);
SetInputScope(val);
}
}

private void SetInputScope(InputScopeNameValue val)
{
try
{
txtInput.InputScope = new System.Windows.Input.InputScope()
{
Names = { new InputScopeName() { NameValue = val } }
};
}
catch (Exception)
{
//Don't do anything. This fails with some of the InputScopes // Bug?
}
}



No comments:

Post a Comment