WindowPhone按键验证

本文介绍了一个用于Windows Phone应用的自定义NumericTextBox控件。该控件继承自TextBox,通过重写OnKeyDown方法限制了用户只能输入数字,并且能够响应返回键。此外,还设置了输入范围为电话号码模式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 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
            }
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值