c# - 如何使用 UWP NumberBox 实现 NumberFormatter

标签 c# xaml uwp

我的 UWP 项目中有一个 NumberBox 控件。

我想用它来显示分钟数,所以我希望盒子上的数字后跟“分钟”(例如“10 分钟”)。

我以为我可以使用 NumberFormatter 来实现它,但不知道如何实现。

这是我尝试过的:

  1. 添加了NumberFormatter
public class DurationFormatter : INumberFormatter2
{
    public string FormatInt(long value)
    {
        return $"{value} min";
    }

    public string FormatDouble(double value)
    {
        return $"{value} min";
    }

    public string FormatUInt(ulong value)
    {
        return $"{value} min";
    }
}
  1. 添加了对页面资源的引用
<Page.Resources>
    <local1:DurationFormatter x:Key="Converter1"/>
</Page.Resources>
  1. 尝试将格式化程序添加到数字框,但不确定如何操作。这行不通:
<muxc:NumberBox x:Name="DurationNumberBox" SpinButtonPlacementMode="Compact" 
                Header="Duration" Value="{x:Bind DurationViewValue, Mode=TwoWay}" 
                ValidationMode="InvalidInputOverwritten" 
                NumberFormatter="{StaticResource Converter1}"/>

感谢任何帮助。

最佳答案

我有一段时间遇到了同样的问题,结果证明问题是转换器类必须实现 INumberFormatter2INumberParser。除 in this comment in the source code of the NumberBox 外,其他地方均未记录此内容.

关于c# - 如何使用 UWP NumberBox 实现 NumberFormatter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63543086/

相关文章:

c# - Roslyn - 查明被调用的方法是否调用了方法

c# - 网格线后面的 OxyPlot AreaSeries

c# - ViewModel 的暴露数据取决于 View 的大小

c# - 在 UWP 中创建/写入/压缩/解压缩文件

c# - 如何将 .NET dll 引用添加到 UWP 项目?

c# - 等同于 asp.net-core 中的 AntiForgery.Validate() 的命令

c# - 当 CanExecute 为 false 时,带有启用主按钮的 uwp MVVM ContentDialog

c# - 引用和读取 .sqlite 数据库文件 (C#/UWP)

c# - Xaml 条目 MVVM 数据绑定(bind)

c# - UWP 删除 Gridview 中的焦点边框