繁体   English   中英

我无法绑定图像源

[英]I couldnt bind Image source

这里是绑定:

<Image Width="16" Height="16"  Source="{Binding SwitchForImage, Converter={StaticResource stringToImage}}" HorizontalAlignment="Left">
</Image>

这是转换器

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string type = (string)value;
        BitmapImage logo = new BitmapImage();
        logo.BeginInit();
        logo.UriSource = new Uri(@"pack://application:,,,/Resources/"+type+@"Icon.png");
        logo.EndInit();
        return logo;
    }

当我运行时,出现此异常“无法找到资源'resources / * icon.png'”。但是我将png文件放入名为Resources的文件夹中。我正在创建库。这些全部在库中。为了测试,我使用了它,然后发生了这个问题。

我将png文件添加为“包含到项目”。构建操作是“内容”。但是我也尝试了其他文件(资源,嵌入式资源)

[回答]我将“构建操作”更改为“资源”,并更改了“转换”,如下所示:

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return "/AutoComplete;component/Resources/" + (string)value + "Icon.png";
    }

一切正常。

我将Build Action更改为Resource,并将Convert方法更改为:

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    return "/AutoComplete;component/Resources/" + (string)value + "Icon.png";
}

暂无
暂无

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

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