0

I have a .dll with a lot of ResourceDictionaries.

The build action of all these ResourceDictionaries is set to "Page".

Inside the Dll, I want to find all these ResourceDictionaries and iterate over them.

If I set the build action to "EmbeddedResource", I can use Reflection:

var embeddedResources = Assembly.GetExecutingAssembly().GetManifestResourceNames().ToList();

But GetManifestResourceNames() does not work for resources with build action "Page".

How do I find or iterate over all resources that have the build action "page"?

The solution doesn't have to be Reflection. Any other way is very welcome.

Thank you!

Solution:

Ladies and Gentleman! I have to announce, that the man of the week and the winner of this bounty, is Mr. Jon Wu. Jon Wu gave the right hint and through searching, I found this solution:

Enumerating .NET assembly resources at runtime

The working code, slightly changed looks like this:

public static string[] GetResourceNames()
    {
        var asm = Assembly.GetExecutingAssembly();
        string resName = asm.GetName().Name + ".g.resources";
        using (var stream = asm.GetManifestResourceStream(resName))
        using (var reader = new System.Resources.ResourceReader(stream))
        {
            return reader.Cast<DictionaryEntry>().Select(entry => (string)entry.Key).ToArray();
        }
    }

If you call this method, you get all the resource strings with a ".baml" at the end and you can iterate over them.

Thank you Jon Wu for the right hint.

8
  • Is this your answer? stackoverflow.com/questions/3314140/… Commented Jun 21, 2017 at 1:00
  • @JohnPeters: No. As I explicitly wrote in the question, GetManifestResourceNames() only works for "EmbeddedResource". It does NOT work for "Page". Commented Jun 21, 2017 at 2:00
  • 1
    All "Page" marked items will go in a <WpfApplicationName>.g.resources .NET embedded resource that contains one stream per dictionary. These dictionaries are in the BAML format (en.wikipedia.org/wiki/…) so I'm not sure if this is useful. What would you want to do once you have enumerated these BAML streams? Commented Jun 23, 2017 at 16:05
  • @SimonMourier: I load them into RAM with "obj = Application.LoadComponent(uri);". The problem is, once you compile them as EmbeddedResource, this doesn't work anymore, if the ResourceDictionaries have references to each other. With references, I mean imports with Uri-Packs. For example: You have a style RD, that imports colors from another RD with: <ResourceDictionary Source="/Styles;component/Gray/Colors.xaml"/>. Commented Jun 23, 2017 at 23:24
  • 1
    You should have told us you only needed the dictionary names. My initial comment gave the solution first Commented Jun 24, 2017 at 9:52

1 Answer 1

4
+50

According to this answer,

Page (WPF only): Used to compile a xaml file into baml. The baml is then embedded with the same technique as Resource (i.e. available as AppName.g.resources).

So it sounds like you just need to look for resources identified with YourAppName.g.resources.

Sign up to request clarification or add additional context in comments.

2 Comments

LADIES AND GENTLEMAN! I have to announce, that the men of week and the winner of this bounty, are Mr. Jon Wu and Mr.Thomas Levesque. Jon Wu gave the right hint and through searching, I found this solution: stackoverflow.com/questions/2517407/…. I have added the solution to the question. Thank you, John Wu!
You get the bounty, but according to the rules, I have to wait 2 hours, before I can click on the award button.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.