【发布时间】:2019-10-17 05:58:36
【问题描述】:
我正在将 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); }
}
【问题讨论】:
-
是的,好像 float
DependencyProperty不能在 XAML 中设置。你为什么不去加倍呢? -
谢谢。该组件是多平台的,因此对于旧平台会有向后兼容性,除非包装在特定于平台的标志下。这很费力,但可行。关于 ScrollViewer 如何浮动的任何见解?