首页 > 解决方案 > uwp xaml 中是否支持浮点类型?

问题描述

我正在将 C# WPF 组件转换为 UWP,它包含具有浮点类型属性的类。

尝试在 xaml 中设置值时,Visual Studio xaml 编辑器抱怨:“无法将 'System.Double' 类型的对象转换为 'System.Single' 类型。运行时:无法转换为 Windows.Foundation.Single。

双打工作正常。

UWP ScrollViewer 具有例如 MinZoomFactor 属性,它是浮动的,它不会产生任何问题。

UIElement 也有 Rotation 属性,它是浮动的。

所以有人会猜测浮动应该是可能的,但如果有的话,怎么做?

阅读一些文档,这表明不支持浮动,这对于 ScrollViewer 和 UIElement 来说有点奇怪。

https://docs.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/xaml-cust-ctrl

这是来自测试项目如何定义浮动属性。

public static DependencyProperty ValueProperty =
    DependencyProperty.Register(
        "Value",
        typeof(float),
        typeof(FloatContainer),
        new PropertyMetadata(0.0f, new PropertyChangedCallback(ValueChanged)));

public float Value
{
    get { return (float)GetValue(ValueProperty); }
    set { SetValue(ValueProperty, value); }
}

标签: c#xamluwp

解决方案


uwp xaml 中是否支持浮点类型?

现在, 的源代码ScrollviewerGitHub中可用。ScrollViewer 在处理其属性的 float 和 dobule 类型时似乎具有独特的机制。

如您的情况所述。

如果您想要一个浮点类型的 DP,则将其设为双精度(MIDL 3.0 中的双精度)。声明和实现浮点类型的 DP(MIDL 中的单),然后在 XAML 标记中设置该 DP 的值,会导致错误无法从文本“”创建“Windows.Foundation.Single”。

我们仍然建议使用双重依赖属性。


推荐阅读