【问题标题】:Call a function from UserControl - UWP C#从 UserControl 调用函数 - UWP C#
【发布时间】:2018-08-01 19:17:05
【问题描述】:

我正在使用以下代码在我的应用程序中旋转图像(使用 UserControl)。但它显示错误ConvertToBitmapImage 未在类型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));

【问题讨论】:

  • 不记得是否可以将数据绑定到方法,但是您可以尝试创建另一个 DependencyProperty,然后用 ConvertToBitmapImage 将其包装在内部,但不确定是否可以使用
  • 我刚刚复制粘贴了您的示例,它工作正常。也许创建一个新项目并做同样的事情?
  • @JetChopper 试过了。但没有运气。我的最低目标版本是 10240

标签: c# uwp windows-10-universal


【解决方案1】:

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

要通过 {x:Bind} 使用函数,您的应用的最低目标 SDK 版本必须为 14393 或更高版本。当您的应用面向早期版本的 Windows 10 时,您无法使用函数。

因此,您应该将应用最小目标版本更改为 14393 或更高版本,或者不要使用 x:bind 函数。

【讨论】:

  • 我以为我读到了您可以绑定到更高版本的 Windows 10 SDK 中的函数,但找不到此参考,感谢您直接分享指向它的链接!
猜你喜欢
  • 2019-01-01
  • 2015-05-17
  • 1970-01-01
  • 2013-07-07
  • 2021-04-21
  • 2016-03-23
  • 2013-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多