public partial class MainPage : PhoneApplicationPage
{
// 构造函数
public MainPage()
{
InitializeComponent();
NumericTextBox a = new NumericTextBox();
ContentPanel.Children.Add(a);
}
public class NumericTextBox : TextBox
{
//返回键和数字键
private readonly Key[] numeric = new Key[] { Key.Back, Key.D0, Key.D1, Key.D2, Key.D3, Key.D4, Key.D5, Key.D6, Key.D7, Key.D8, Key.D9 };
public NumericTextBox()
{
//将文本设置为 电话号码的文本输入模式
this.InputScope = new InputScope();
this.InputScope.Names.Add(new InputScopeName() { NameValue = InputScopeNameValue.Number });
}
protected override void OnKeyDown(KeyEventArgs e)
{
//如果是数字键或者返回键则设置e.Handled = true; 表示事件已经处理
if (Array.IndexOf(numeric, e.Key) == -1)
{ e.Handled = true; }
base.OnKeyDown(e); // important, if not called the back button is not handled
}
}
{
// 构造函数
public MainPage()
{
InitializeComponent();
NumericTextBox a = new NumericTextBox();
ContentPanel.Children.Add(a);
}
public class NumericTextBox : TextBox
{
//返回键和数字键
private readonly Key[] numeric = new Key[] { Key.Back, Key.D0, Key.D1, Key.D2, Key.D3, Key.D4, Key.D5, Key.D6, Key.D7, Key.D8, Key.D9 };
public NumericTextBox()
{
//将文本设置为 电话号码的文本输入模式
this.InputScope = new InputScope();
this.InputScope.Names.Add(new InputScopeName() { NameValue = InputScopeNameValue.Number });
}
protected override void OnKeyDown(KeyEventArgs e)
{
//如果是数字键或者返回键则设置e.Handled = true; 表示事件已经处理
if (Array.IndexOf(numeric, e.Key) == -1)
{ e.Handled = true; }
base.OnKeyDown(e); // important, if not called the back button is not handled
}
}