从 UserControl 调用函数 - UWP C#

Call a function from UserControl - UWP C#

我正在使用以下代码在我的应用程序中旋转图像(使用 UserControl)。但它显示错误 ConvertToBitmapImage was not found in type ImageControl。我该如何解决?

ImageControl XAML:

<UserControl x:Class="App1.ImageControl" ...>
    <Image RenderTransformOrigin="0.5,0.5"
           Source="{x:Bind ConvertToBitmapImage(UriPath), Mode=OneWay}"
           Stretch="UniformToFill">
        <Image.RenderTransform>
            <CompositeTransform Rotation="{x:Bind Angle, Mode=OneWay}" />
        </Image.RenderTransform>
    </Image>
</UserControl>

ImageControl 背后的代码:

public string UriPath
{
    get => (string)GetValue(UriPathProperty);
    set => SetValue(UriPathProperty, value);
}

public static readonly DependencyProperty UriPathProperty = DependencyProperty.Register("UriPath", typeof(string), typeof(ImageControl), new PropertyMetadata(default(string)));

public double Angle
{
    get => (double)GetValue(AngleProperty);
    set => SetValue(AngleProperty, value);
}

public static readonly DependencyProperty AngleProperty = DependencyProperty.Register("Angle", typeof(double), typeof(ImageControl), new PropertyMetadata(default(double)));

public BitmapImage ConvertToBitmapImage(string path) => new BitmapImage(new Uri(BaseUri, path));

在 Windows 10,版本 1607 之前,您不能使用 {x:Bind} 绑定函数。请参阅 Functions in binding paths 注释部分:

To use functions with {x:Bind}, your app's minimum target SDK version must be 14393 or later. You can't use functions when your app targets earlier versions of Windows 10.

因此,您应该将应用的最低目标版本更改为 14393 或更高版本,或者不要使用 x:bind 功能。