【问题标题】:Cast of a Double value to a Single in {x:Bind}在 {x:Bind} 中将 Double 值强制转换为 Single
【发布时间】:2021-07-08 12:59:58
【问题描述】:

我正在将使用 {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.

【问题讨论】:

    标签: .net xaml uwp uwp-xaml


    【解决方案1】:

    我找到了解决方案:在我的 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 会自动完成它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多