簡體   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