在 {x:Bind} 中将 Double 值转换为 Single
Cast of a Double value to a Single in {x:Bind}
我正在将使用 {Binding}
标记扩展的代码移植到更现代的 {x:Bind}
并在构建我的项目时遇到以下错误:
Cannot directly bind type 'System.Double' to 'System.Single'. Use a cast, converter or function binding to change the type
我的代码如下:
<Grid>
<TextBlock Text="Rotation"
Rotation="{x:Bind slider.Value}"
x:DefaultBindMode="OneWay"
FontWeight="Bold"
FontSize="36"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
<Slider x:Name="slider"
Maximum="360"
VerticalAlignment="Stretch" />
</Grid>
我可以看到 Slider.Value
被定义为 Double
,其中 TextBlock.Rotation
是一个浮点数。将 slider.Value
转换为 Single
的正确方法是什么?
我试过这样转换 {x:Bind (x:Single)slider.Value}
但构建失败并出现错误:
Invalid binding path '(x:Single)slider.Value' : Type 'x:Single' not found.
我找到了一个解决方案:在我的 XAML 文件中导入命名空间 System
。虽然我不确定这是否是一个好方法。
<Page x:Class="TodoUWP.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:sys="using:System"
> <!-- here import `System` namespace -->
<Grid>
<TextBlock Text="Rotation"
x:DefaultBindMode="OneWay"
FontWeight="Bold"
FontSize="36"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Rotation="{x:Bind (sys:Single)slider.Value}" />
<!-- -->
<!-- then cast with `sys:Single` instead of `x:Single` -->
<Slider x:Name="slider"
Maximum="360"
VerticalAlignment="Stretch" />
</Grid>
</Page>
我仍然不确定为什么 x:Single
无效,Visual Studio 会自动完成它。
我正在将使用 {Binding}
标记扩展的代码移植到更现代的 {x:Bind}
并在构建我的项目时遇到以下错误:
Cannot directly bind type 'System.Double' to 'System.Single'. Use a cast, converter or function binding to change the type
我的代码如下:
<Grid>
<TextBlock Text="Rotation"
Rotation="{x:Bind slider.Value}"
x:DefaultBindMode="OneWay"
FontWeight="Bold"
FontSize="36"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
<Slider x:Name="slider"
Maximum="360"
VerticalAlignment="Stretch" />
</Grid>
我可以看到 Slider.Value
被定义为 Double
,其中 TextBlock.Rotation
是一个浮点数。将 slider.Value
转换为 Single
的正确方法是什么?
我试过这样转换 {x:Bind (x:Single)slider.Value}
但构建失败并出现错误:
Invalid binding path '(x:Single)slider.Value' : Type 'x:Single' not found.
我找到了一个解决方案:在我的 XAML 文件中导入命名空间 System
。虽然我不确定这是否是一个好方法。
<Page x:Class="TodoUWP.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:sys="using:System"
> <!-- here import `System` namespace -->
<Grid>
<TextBlock Text="Rotation"
x:DefaultBindMode="OneWay"
FontWeight="Bold"
FontSize="36"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Rotation="{x:Bind (sys:Single)slider.Value}" />
<!-- -->
<!-- then cast with `sys:Single` instead of `x:Single` -->
<Slider x:Name="slider"
Maximum="360"
VerticalAlignment="Stretch" />
</Grid>
</Page>
我仍然不确定为什么 x:Single
无效,Visual Studio 会自动完成它。