Windows Phone开发(13):如何规范用户的输入行为

移动开发
Windows Phone是微软发布的一款手机操作系统,它将微软旗下的Xbox Live游戏、Xbox Music音乐与独特的视频体验整合至手机中。

很多时候,我们对用户的操作或输入做一定程度的限制,以避免发生不必要的异常或错误,因此,对一些特殊的类型,进行输入限制是很有必要的。

通改变屏幕键盘的显示模式可以规范和告诉用户, 这里应该输入什么。

要做到这些并不复杂,只要用到一个类就足矣—— InputScope,它有一个Names属性,包含InputScopeName列表,而InputScopeName的NameValue是一个枚举值,说到底,显示什么样的屏幕键盘就取决于该枚举值。

 

非常多的内容,至少每个枚举项表示什么,就没有必要一一说明,大家参考SDK文档就可以了。

 

上面是理论部分,是不是这么神呢,下面我们用实践来证明吧。

 

启动你的VS,然后新建一个Silverlight for Windows Phone应用项目。

界面很简单,随便放几个TextBox,我们只做几个典型的应用就行了,因为原理都是一样的。比如数字,日期,时间,Email地址等。

  1. <phone:PhoneApplicationPage  
  2.     x:Class="InputScpSample.MainPage" 
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  5.     xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
  6.     xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" 
  7.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  8.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  9.     mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768" 
  10.     FontFamily="{StaticResource PhoneFontFamilyNormal}" 
  11.     FontSize="{StaticResource PhoneFontSizeNormal}" 
  12.     Foreground="{StaticResource PhoneForegroundBrush}" 
  13.     SupportedOrientations="Portrait" Orientation="Portrait" 
  14.     shell:SystemTray.IsVisible="True" xmlns:my="clr-namespace:System;assembly=mscorlib"
  15.     <phone:PhoneApplicationPage.Resources> 
  16.         <my:Double x:Key="fontSize">32</my:Double> 
  17.     </phone:PhoneApplicationPage.Resources> 
  18.     <!--LayoutRoot 是包含所有页面内容的根网格--> 
  19.     <Grid x:Name="LayoutRoot" Background="Transparent"
  20.         <Grid.RowDefinitions> 
  21.             <RowDefinition Height="Auto"/> 
  22.             <RowDefinition Height="*"/> 
  23.         </Grid.RowDefinitions> 
  24.         <!--TitlePanel 包含应用程序的名称和页标题--> 
  25.         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"
  26.             <TextBlock x:Name="ApplicationTitle" Text="我的应用程序" Style="{StaticResource PhoneTextNormalStyle}"/> 
  27.             <TextBlock x:Name="PageTitle" Text="规范输入" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> 
  28.         </StackPanel> 
  29.  
  30.         <!--ContentPanel - 在此处放置其他内容--> 
  31.         <ScrollViewer x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" 
  32.                       HorizontalScrollBarVisibility="Auto" 
  33.                       VerticalScrollBarVisibility="Auto"
  34.             <StackPanel> 
  35.                 <TextBlock Text="输入Email地址:" FontSize="{StaticResource fontSize}" /> 
  36.                 <TextBox Name="txtEmail" FontSize="{StaticResource fontSize}" HorizontalAlignment="Stretch"
  37.                     <TextBox.InputScope> 
  38.                         <InputScope> 
  39.                             <InputScope.Names> 
  40.                                 <InputScopeName NameValue="EmailNameOrAddress"/> 
  41.                             </InputScope.Names> 
  42.                         </InputScope> 
  43.                     </TextBox.InputScope> 
  44.                 </TextBox> 
  45.                 <TextBlock Text="输入数字:" Margin="0,15,0,0" FontSize="{StaticResource fontSize}" /> 
  46.                 <TextBox Name="txtNumber" FontSize="{StaticResource fontSize}" HorizontalAlignment="Stretch"
  47.                     <TextBox.InputScope> 
  48.                         <InputScope> 
  49.                             <InputScope.Names> 
  50.                                 <InputScopeName NameValue="Number"/> 
  51.                             </InputScope.Names> 
  52.                         </InputScope> 
  53.                     </TextBox.InputScope> 
  54.                 </TextBox> 
  55.                 <TextBlock Text="输入时间:" Margin="0,15,0,0" FontSize="{StaticResource fontSize}" /> 
  56.                 <TextBox Name="txtTime" FontSize="{StaticResource fontSize}" HorizontalAlignment="Stretch"
  57.                     <TextBox.InputScope> 
  58.                         <InputScope> 
  59.                             <InputScope.Names> 
  60.                                 <InputScopeName NameValue="Time"/> 
  61.                             </InputScope.Names> 
  62.                         </InputScope> 
  63.                     </TextBox.InputScope> 
  64.                 </TextBox> 
  65.                 <TextBlock Text="输入人民币:" Margin="0,15,0,0" FontSize="{StaticResource fontSize}" /> 
  66.                 <TextBox Name="txtRMB" FontSize="{StaticResource fontSize}" HorizontalAlignment="Stretch"
  67.                     <TextBox.InputScope> 
  68.                         <InputScope> 
  69.                             <InputScope.Names> 
  70.                                 <InputScopeName NameValue="CurrencyChinese"/> 
  71.                             </InputScope.Names> 
  72.                         </InputScope> 
  73.                     </TextBox.InputScope> 
  74.                 </TextBox> 
  75.                <TextBlock Text="输入电话号码:" Margin="0,15,0,0" FontSize="{StaticResource fontSize}" /> 
  76.                 <TextBox Name="txtPhoneNum" FontSize="{StaticResource fontSize}" HorizontalAlignment="Stretch"
  77.                     <TextBox.InputScope> 
  78.                         <InputScope> 
  79.                             <InputScope.Names> 
  80.                                 <InputScopeName NameValue="TelephoneNumber"/> 
  81.                             </InputScope.Names> 
  82.                         </InputScope> 
  83.                     </TextBox.InputScope> 
  84.                 </TextBox> 
  85.                 <TextBlock Text="输入URL:" Margin="0,15,0,0" FontSize="{StaticResource fontSize}" /> 
  86.                 <TextBox Name="txtURL" FontSize="{StaticResource fontSize}" HorizontalAlignment="Stretch"
  87.                     <TextBox.InputScope> 
  88.                         <InputScope> 
  89.                             <InputScope.Names> 
  90.                                 <InputScopeName NameValue="Url"/> 
  91.                             </InputScope.Names> 
  92.                         </InputScope> 
  93.                     </TextBox.InputScope> 
  94.                 </TextBox> 
  95.  
  96.                 <TextBlock Text="输入单个字符:" Margin="0,15,0,0" FontSize="{StaticResource fontSize}" /> 
  97.                 <TextBox Name="txtChat" FontSize="{StaticResource fontSize}" HorizontalAlignment="Stretch"
  98.                     <TextBox.InputScope> 
  99.                         <InputScope> 
  100.                             <InputScope.Names> 
  101.                                 <InputScopeName NameValue="Chat"/> 
  102.                             </InputScope.Names> 
  103.                         </InputScope> 
  104.                     </TextBox.InputScope> 
  105.                 </TextBox> 
  106.  
  107.             </StackPanel> 
  108.         </ScrollViewer> 
  109.     </Grid> 
  110.     <!--演示 ApplicationBar 用法的示例代码--> 
  111.     <!--<phone:PhoneApplicationPage.ApplicationBar> 
  112.         <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True"
  113.             <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="按钮 1"/> 
  114.             <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="按钮 2"/> 
  115.             <shell:ApplicationBar.MenuItems> 
  116.                 <shell:ApplicationBarMenuItem Text="菜单项 1"/> 
  117.                 <shell:ApplicationBarMenuItem Text="菜单项 2"/> 
  118.             </shell:ApplicationBar.MenuItems> 
  119.         </shell:ApplicationBar> 
  120.     </phone:PhoneApplicationPage.ApplicationBar>--> 
  121. </phone:PhoneApplicationPage> 

当然了,这种限制并不能十分准确,所以在你应用程序中,确认的时候也应当对特定的数据类型进行多一次验证。

责任编辑:闫佳明 来源: oschina
相关推荐

2021-07-28 09:53:53

FalconEye注入安全检测

2010-10-11 14:42:49

Windows Pho

2013-07-30 12:37:56

Windows PhoWindows Pho

2010-04-21 17:07:54

Windows Pho

2012-05-28 09:54:09

APP 性能

2011-06-07 12:42:15

Windows Pho

2013-04-17 14:00:06

Windows PhoWindows Pho

2013-04-16 17:02:50

Windows Pho概论

2013-04-19 16:34:56

Windows PhoWindows Pho

2013-07-30 11:18:37

Windows PhoWindows Pho

2010-04-08 17:40:23

Windows Pho

2010-07-16 15:29:02

Windows Pho

2013-04-17 14:47:19

Windows PhoWindows Pho

2012-08-16 10:35:50

Windows Pho

2013-04-17 13:27:04

Windows PhoWindows Pho

2013-07-31 13:03:51

Windows PhoWindows Pho

2011-06-07 11:35:38

Windows Pho

2013-04-19 15:35:54

Windows Pho隔离存储

2010-12-14 18:48:49

微软

2012-06-04 14:47:58

Windows Pho
点赞
收藏

51CTO技术栈公众号