加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

Windows 8应用程序中Numericbox的替代品?

发布时间:2020-12-14 04:14:57 所属栏目:Windows 来源:网络整理
导读:我试图在 Windows 8磁贴应用程序中找到一个很好的替代Numericbox.我尝试使用与Windows窗体相同的数字框,但得到一个错误,说明Windows 8应用程序不支持这些(?).我注意到tile应用程序的TextBox元素有一个可以设置为“Number”的InputScope,但它仍然允许用户键
我试图在 Windows 8磁贴应用程序中找到一个很好的替代Numericbox.我尝试使用与Windows窗体相同的数字框,但得到一个错误,说明Windows 8应用程序不支持这些(?).我注意到tile应用程序的TextBox元素有一个可以设置为“Number”的InputScope,但它仍然允许用户键入他想要的任何字符.我假设InputScope没有做我认为它做的事情.

我目前正在管理文本框,但因为我正在进行计算,所以当我想要更新界面时,文本必须不断转换为十进制,然后返回文本,此外还必须执行多项检查以确保用户确实不输入非数字字符.这变得非常乏味,并且非常熟悉Windows Form,这似乎是朝着错误方向迈出的一步.我一定错过了一些明显的东西?

我不熟悉NumericTextBox,但这是一个简单的C#/ XAML实现,只允许数字和小数字符.

它只是覆盖OnKeyDown事件;基于被按下的键,它允许或不允许事件到达基本TextBox类.

我应该注意,这个实现适用于Windows应用商店应用 – 我相信你的问题是关于那种类型的应用,但我不是100%肯定.

public class MyNumericTextBox : TextBox
{
    protected override void OnKeyDown(KeyRoutedEventArgs e)
    {
        HandleKey(e);

        if (!e.Handled)
            base.OnKeyDown(e);
    }

    bool _hasDecimal = false;
    private void HandleKey(KeyRoutedEventArgs e)
    {
        switch (e.Key)
        {
            // allow digits
            // TODO: keypad numeric digits here
            case Windows.System.VirtualKey.Number0:
            case Windows.System.VirtualKey.Number1:
            case Windows.System.VirtualKey.Number2:
            case Windows.System.VirtualKey.Number3:
            case Windows.System.VirtualKey.Number4:
            case Windows.System.VirtualKey.Number5:
            case Windows.System.VirtualKey.Number6:
            case Windows.System.VirtualKey.Number7:
            case Windows.System.VirtualKey.Number8:
            case Windows.System.VirtualKey.Number9:
                e.Handled = false;
                break;

            // only allow one decimal
            // TODO: handle deletion of decimal...
            case (Windows.System.VirtualKey)190:    // decimal (next to comma)
            case Windows.System.VirtualKey.Decimal: // decimal on key pad
                e.Handled = (_hasDecimal == true);
                _hasDecimal = true;
                break;

            // pass various control keys to base
            case Windows.System.VirtualKey.Up:
            case Windows.System.VirtualKey.Down:
            case Windows.System.VirtualKey.Left:
            case Windows.System.VirtualKey.Right:
            case Windows.System.VirtualKey.Delete:
            case Windows.System.VirtualKey.Back:
            case Windows.System.VirtualKey.Tab:
                e.Handled = false;
                break;

            default:
                // default is to not pass key to base
                e.Handled = true;
                break;
        }
    }
}

这是一些示例XAML.请注意,它假定MyNumericTextBox位于项目命名空间中.

<StackPanel Background="Black">
    <!-- custom numeric textbox -->
    <local:MyNumericTextBox />
    <!-- normal textbox -->
    <TextBox />
</StackPanel>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读