【问题标题】:ImageBrush data binding in UWP gives error when ImageSource is null当 ImageSource 为空时,UWP 中的 ImageBrush 数据绑定会出错
【发布时间】:2017-05-11 06:34:36
【问题描述】:

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

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

我想要实现的是将 ImageSource 绑定到 ViewModel 之类的

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

但是,每当 ImageUrl 为 null 时,我都会遇到异常

{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

【问题讨论】:

  • x:Bind --- 是错字吗?
  • 不,UWP 允许 x:Bind 的这种新语法与 Binding 一起使用

标签: c# xaml data-binding uwp uwp-xaml


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2014-05-01
    • 1970-01-01
    • 2016-02-02
    • 2017-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-14
    • 1970-01-01
    相关资源
    最近更新 更多