不,这不只是关于包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}" />
我使用大量不同的资源,字符串,颜色(实际上是应用程序),一切都有效,但图像除外。
答案 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}" />