问题描述
是否可以枚举Assembly中定义的所有XAML资源?如果您有可用的密钥,我知道如何检索资源,但是在我的情况下情况并非如此.
Is it possible to enumerate all XAML resources defined in an Assembly? I know how to retrieve a resource if you have it's Key available, but it isn't the case in my situation.
好像我还不够清楚.我想列出我知道完整路径的外部程序集中定义的XAML资源.
Seems like I wasn't clear enough. I want to list XAML resources defined in an external Assembly that I know full path to.
推荐答案
是的,您可以通过循环迭代资源.例如,使用 foreach
循环:
yeah, you can iterate resources through loops. For example, using foreach
loop:
foreach (var res in Application.Current.Resources)
{
Console.WriteLine(res);
}
更新:
要从外部库获取所有 ResourceDictionary'ies
,首先应加载该库,然后获取 ManifestResourceInfo
.让我举个例子:
To get all ResourceDictionary'ies
from external library, you should, at first, load the library, then get ManifestResourceInfo
. Let me show an example:
string address = @"WpfCustomControlLibrary.dll";
List<Stream> bamlStreams = new List<Stream>();
Assembly skinAssembly = Assembly.LoadFrom(address);
string[] resourceDictionaries = skinAssembly.GetManifestResourceNames();
foreach (string resourceName in resourceDictionaries)
{
ManifestResourceInfo info = skinAssembly.GetManifestResourceInfo(resourceName);
if (info.ResourceLocation != ResourceLocation.ContainedInAnotherAssembly)
{
Stream resourceStream = skinAssembly.GetManifestResourceStream(resourceName);
using (ResourceReader reader = new ResourceReader(resourceStream))
{
foreach (DictionaryEntry entry in reader)
{
//Here you can see all your ResourceDictionaries
//entry is your ResourceDictionary from assembly
}
}
}
}
您可以在 reader
中看到所有的 ResourceDictionary
.请查看上面的代码.
You can see all your ResourceDictionary
's in reader
. Please, see the above code.
我已经测试了此代码,并且可以正常工作.
I've tested this code and it works.
这篇关于如何获取在Assembly中定义的XAML资源列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!