存储在外部资源DLL中的图像

时间:2016-11-28 10:04:35

标签: wpf

不,这不只是关于包URI的另一个问题。 : - )

我在应用程序外部有一个资源DLL来存储品牌数据。它在BitmapImage中存储ResourceDictionary(请注意,每个品牌的原始图像文件名都不同,因此对于每个DLL):

<BitmapImage x:Key="Logo" UriSource="Resources/changing-vendorname.png" />

在主应用程序中,我读取外部DLL并从中获取各种资源,包括供应商徽标:

Assembly.LoadFrom(BrandDllPath);
var dict = new ResourceDictionary();
dict.Source = new Uri("pack://application:,,,/BrandDll;component/Themes/Generic.xaml");
var VendorLogo = (BitmapImage)dict["Logo"];

当我检查这个VendorLogo变量时,它正如预期的那样包含BitmapImage。但是,当我想在Image控件中显示它时:

Image.Source = VendorLogo;

我一无所获。没有错误消息,只显示任何内容。

实际上,如果我将资源放在我自己的资源中,也会发生同样的事情:

Application.Current.Resources["Logo"] = (BitmapImage)dict["Logo"];

并尝试在XAML中使用它:

<Image Source="{DynamicResource Logo}" />

我使用大量不同的资源,字符串,颜色(实际上是应用程序),一切都有效,但图像除外。

1 个答案:

答案 0 :(得分:0)

克莱门斯给了我一个可以变成一个有效解决方案的想法。而不是

var VendorLogo = (BitmapImage)dict["Logo"];

我们有一个多步骤流程,可以获取原始图像名称并使用它:

var logo = (BitmapImage)dict["Logo"];
string uri = "pack://application:,,,/BrandDll;component/" + logo.UriSource;
var VendorLogo = new BitmapImage(new Uri(uri));
// or
Application.Current.Resources["Logo"] = new BitmapImage(new Uri(uri));

并像这样使用它:

<Image Source="{DynamicResource Logo}" />