C# 在 Windows Phone 中打开数字键盘?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9690708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 08:26:22  来源:igfitidea点击:

Open numeric only keyboard in Windows Phone?

c#silverlightwindows-phone-7.1

提问by Cippo

How can i set the keyboard to open in number mode or directly open a special numeric keyboard (as in android)??? My goal is to avoid the user to press the little button to toggle letters and numbers everytime before entering the value that may only be a numerical value. I have a textbox that the user needs to edit.Thanks!!!!

如何将键盘设置为以数字模式打开或直接打开特殊数字键盘(如在 android 中)???我的目标是避免用户每次在输入可能只是数值的值之前按下小按钮来切换字母和数字。我有一个用户需要编辑的文本框。谢谢!!!!

采纳答案by David Ruttka

Set the InputScopeto Number.

将 设置InputScopeNumber

XAML

XAML

<TextBox InputScope="Number" Name="txtPhoneNumber" />

C#

C#

InputScope scope = new InputScope();
InputScopeName name = new InputScopeName();

name.NameValue = InputScopeNameValue.Number;
scope.Names.Add(name);

txtPhoneNumber.InputScope = scope;

Above code snippets taken from this MSDN articlewhich you can review for more information. As Martin added in the comment, you can also see screenshots of the different InputScope options here.

以上代码片段取自此 MSDN 文章,您可以查看以获取更多信息。正如 Martin 在评论中添加的那样,您还可以在此处查看不同 InputScope 选项的屏幕截图。

回答by Cedric Michel

InputScope="Number"  

TextBox numercicTextBox = new TextBox(); 
// ...propriétés de la textbox à initialiser + l'ajouter dans le ContentPanel
numercicTextBox.KeyDown += new KeyEventHandler(numercicTextBox_KeyDown);

 void numercicTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (System.Text.RegularExpressions.Regex.IsMatch(e.Key.ToString(),"[0-9]"))
                e.Handled = false;
            else e.Handled = true;

        }