Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

c# - How to get a list of XAML resources defined in an Assembly?

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.

EDIT: Seems like I wasn't clear enough. I want to list XAML resources defined in an external Assembly that I know full path to.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

yeah, you can iterate resources through loops. For example, using foreach loop:

foreach (var res in Application.Current.Resources)
{
     Console.WriteLine(res);
}

Update:

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
          }
      }
    }
}

You can see all your ResourceDictionary's in reader. Please, see the above code.

I've tested this code and it works.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...