Image Converter无法在Xamarin.Forms中使用

时间:2019-02-08 09:15:56

标签: c# xamarin xamarin.forms

我正在使用转换器来更改图像,但是图像未在此处加载,可以有人帮我在这里做错什么吗?我已经调试了转换器,但没有遇到问题。

转换器代码:

public class DownloadIconConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return ((bool)value) ? "progressicon.png" : "cloud_download.png";
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    }
}

在Xaml中

  <ContentPage.Resources>
    <converter:DownloadIconConverter x:Key="downloadiconconverter" />
</ContentPage.Resources>

  <ImageButton
                                HeightRequest="40"  
                                    IsVisible="True"

                                Source="{Binding DownloadIcon,Converter={StaticResource downloadiconconverter}}"
                                WidthRequest="35" Command ="{Binding BindingContext.Download, Source={x:Reference listview}}" CommandParameter="{Binding .}" />

在文件后面的代码中

    public bool DownloadIcon
    {
        get { return downloadicon; }
        set { SetProperty(ref downloadicon, value); }
    }

1 个答案:

答案 0 :(得分:0)

据我所知,绑定不起作用,因为布尔值位于ViewModel中,并且列表视图数据模板无法访问

尝试使用此:

 Source="{Binding BindingContext.DownloadIcon, Converter={StaticResource downloadiconconverter},Source={x:Reference listview}}"