简体   繁体   English

我无法绑定图像源

[英]I couldnt bind Image source

Here is binding: 这里是绑定:

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

and here is the Convertor 这是转换器

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;
    }

when i run ,gives this exception "Cannot locate resource 'resources/*icon.png' ".But i put png files into folder named Resources.I am creating library.These all in library.For test i used it,and then occured this problem. 当我运行时,出现此异常“无法找到资源'resources / * icon.png'”。但是我将png文件放入名为Resources的文件夹中。我正在创建库。这些全部在库中。为了测试,我使用了它,然后发生了这个问题。

I added png files as"include to project".Build action is "Content".But i tried others also(Resource,Embedded resource) 我将png文件添加为“包含到项目”。构建操作是“内容”。但是我也尝试了其他文件(资源,嵌入式资源)

[Answer] I changed Build Action to Resource and changed Convert like that: [回答]我将“构建操作”更改为“资源”,并更改了“转换”,如下所示:

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

And everything work normally. 一切正常。

I changed Build Action to Resource and changed Convert method like that: 我将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