How to get a list of XAML resources defined in an assembly?

Can I list all the XAML resources defined in the assembly? I know how to get a resource if you have a key. The key is available, but in my situation it is not.

EDIT: Looks like I was not clear enough. I want to list the XAML resources defined in an external assembly, for which I know the full path.

+4
source share
2 answers

Yes, you can iterate over resources through loops. For example, using a loop foreach:

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

Update:

To get everything ResourceDictionary'iesfrom an external library, you must first load the library and then get ManifestResourceInfo. Let me show you 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
          }
      }
    }
}

ResourceDictionary reader. , .

, .

+5

:

        ResourceDictionary dictionary = new ResourceDictionary();
        dictionary.Source = new Uri("pack://application:,,,/WpfControlAssembly;Component/RD1.xaml", UriKind.Absolute);
        foreach (var item in dictionary.Values)
        {
           //Operations
        }

WpfControlAssembly - . Component - , RD1.xaml - Resource Dictionary.

:

Resource Dictionary

:

Conclusion

PS: ResourceDictionary Build Action 'Resource' 'Page'.

:

, . :

public ResourceDictionary GetResourceDictionary(string assemblyName)
    {
        Assembly asm = Assembly.LoadFrom(assemblyName);
        Stream stream = asm.GetManifestResourceStream(asm.GetName().Name + ".g.resources");            
        using (ResourceReader reader = new ResourceReader(stream))
        {
            foreach (DictionaryEntry entry in reader)
            {
                var readStream = entry.Value as Stream;
                Baml2006Reader bamlReader = new Baml2006Reader(readStream);
                var loadedObject = System.Windows.Markup.XamlReader.Load(bamlReader);
                if (loadedObject is ResourceDictionary)
                {
                    return loadedObject as ResourceDictionary;
                }
            }
        }
        return null;
    }

:

RD

- Exceptions, , WPF ( ResourceDictionary).

+5

All Articles