不,这不仅是关于包uris的另一个问题。: - )
我有一个应用程序外部的资源DLL来存储品牌数据。它在其ResourceDictionary
中存储一个BitmapImage
(请注意,每个品牌的原始图像文件名都会有所不同,因此每个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}" />
我对许多不同的资源,字符串,颜色(实际上是主题为主题),除了图像外,一切都可以。
clemens给了我一个可以变成有效解决方案的想法。而不是
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}" />