UWP 中的图像画笔数据绑定在图像源为空时出错


在许多

地方都建议可以使用以下代码在 UWP 中使用圆形图像。

<Ellipse Width="250" Height="250">
    <Ellipse.Fill>
        <ImageBrush ImageSource="url" />
    </Ellipse.Fill>
</Ellipse>

我想要实现的是将图像源绑定到视图模型,例如

<Ellipse Width="250" Height="250">
    <Ellipse.Fill>
        <ImageBrush ImageSource="{x:Bind ImageUrl}" />
    </Ellipse.Fill>
</Ellipse>

但是,每当图像网址为空时,我都会收到异常

{System.ArgumentException: The parameter is incorrect.
value
   at Windows.UI.Xaml.Markup.XamlBindingHelper.ConvertValue(Type type, Object value)
   at Views.DetailView.DetailView_obj1_Bindings.Update_ImageUrl(String obj, Int32 phase)
   at Views.DetailView.DetailView_obj1_Bindings.Update_(Episode obj, Int32 phase)
   at Views.DetailView.DetailView_obj1_Bindings.ProcessBindings(ContainerContentChangingEventArgs args)}    System.ArgumentException

这是类型不匹配。ImageSource属性的类型为 ImageSource,您的ImageUrl可能是string。它将使用经典{Binding }而不是{x:Bind },但对于{x:Bind },您需要一个将string转换为ImageSource的转换器(例如,通过创建新BitmapImage(。

最新更新