当 ImageSource 为 null 时,UWP 中的 ImageBrush 数据绑定会出错
ImageBrush data binding in UWP gives error when ImageSource is null
很多地方都建议在 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
这是类型不匹配。 ImageSource
属性 的类型是 ImageSource
而你的 ImageUrl
可能是 string
。它可以使用经典的 {Binding }
而不是 {x:Bind }
但对于 {x:Bind }
你需要一个转换器将 string
转换为 ImageSource
(通过创建一个新的 BitmapImage
例如)。
很多地方都建议在 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
这是类型不匹配。 ImageSource
属性 的类型是 ImageSource
而你的 ImageUrl
可能是 string
。它可以使用经典的 {Binding }
而不是 {x:Bind }
但对于 {x:Bind }
你需要一个转换器将 string
转换为 ImageSource
(通过创建一个新的 BitmapImage
例如)。