简体   繁体   English

ImageBrush ImageSource x:绑定TargetNullValue与datatamplate不同

[英]ImageBrush ImageSource x:Bind TargetNullValue different than datatamplate

So, I have a ListView that is x:Bind to a list and a DataTemplate . 因此,我有一个ListViewx:Bind到一个列表和一个DataTemplate

The DataTemplate has an Elipse filled with an ImageBrush . DataTemplate具有一个用ImageBrush填充的Elipse

Everything works as it should but: 一切正常,但:

If an item in the list has a Null value for its string URL image property, the app crashes. 如果列表中的项目的字符串URL图像属性的值为Null ,则应用程序崩溃。

I have tried setting a TargetNullValue but my problem is that the X:DataType of the template is a class from an API, so I cant control it. 我尝试设置TargetNullValue但是我的问题是模板的X:DataType是API中的类,因此我无法控制它。 I want to have an image as a default value in case the item has an image value of null. 如果项目的图像值为null,我想将图像作为默认值。

In other words, if the item's image URL property is Null , I want XAML to load a predefined image from my Assets folder. 换句话说,如果项目的图像URL属性为Null ,那么我希望XAML从我的Assets文件夹中加载预定义的图像。

The problem is that because I have set my DataType as the class, anything I x:Bind to has to be within that class. 问题是,因为我已经将DataType设置为该类,所以x:Bind要执行的任何操作都必须在该类之内。

<Ellipse Width="40" Height="40">
    <Ellipse.Fill>
        <ImageBrush ImageSource="{x:Bind IconUrl, Mode=OneWay,TargetNullValue=/Assets/NoAvatarIcon.png}"/>
    </Ellipse.Fill>
</Ellipse>

The above for example doesn't work for a Null string in ImageSource as the Path is set to the Class . 上面的示例不适用于ImageSource中的Null字符串,因为Path设置为Class

Right? 对? Any workarounds? 任何解决方法?

The FallbackValue in Binding and x:Bind is different. Binding和x:Bind中的FallbackValue不同。

In Binding, FallbackValue is the value to use when the binding is unable to return a value. 在Binding中,FallbackValue是当绑定无法返回值时要使用的值。

A binding uses FallbackValue for cases where the Path doesn't evaluate on the data source at all, or if attempting to set it on the source with a two-way binding throws an exception that's caught by the data binding engine. 如果路径根本不对数据源求值,或者使用双向绑定在源上进行设置时,绑定使用FallbackValue会引发数据绑定引擎捕获的异常。 FallbackValue is also used if the source value is the dependency property sentinel value DependencyProperty.UnsetValue . 如果源值是依赖项属性哨兵值DependencyProperty.UnsetValue则还使用FallbackValue。

But in x:Bind, FallbackValue specifies a value to display when the source or path cannot be resolved. 但是在x:Bind中,FallbackValue指定了无法解析源或路径时要显示的值。 It can't work with DependencyProperty.UnsetValue. 它不能与DependencyProperty.UnsetValue一起使用。

For your scenario, you can use Converter to operate DependencyProperty.UnsetValue just like following code. 对于您的方案,可以使用Converter来运行DependencyProperty.UnsetValue ,就像下面的代码一样。

public class ImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        object res;        
        res = (value == null ? false : true) ? string.IsNullOrEmpty(value.ToString()) ? null : new BitmapImage(new Uri(value.ToString())) : null;
        return res;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

Usage in Xaml File Xaml文件中的用法

  <Page.Resources>
        <local:ImageConverter x:Key="cm" />
    </Page.Resources>
    <StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <ListView x:Name="MyListView" ItemsSource="{x:Bind Items}">
            <ListView.ItemTemplate>
                <DataTemplate x:DataType="local:HeadPhoto">
                    <Ellipse Width="40" Height="40">
                        <Ellipse.Fill>
                            <ImageBrush ImageSource="{x:Bind PicUri,TargetNullValue=/Assets/pic.png,Converter={StaticResource cm }}" />
                        </Ellipse.Fill>
                    </Ellipse>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackPanel>
</Page>

Placeholder image effect. 占位符图像效果。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM