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

前端 未结 2 1235
后悔当初
后悔当初 2021-01-06 02:01

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 situat

相关标签:
2条回答
  • 2021-01-06 02:19

    Try below code:

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

    Here WpfControlAssembly is name of your assembly.Component is fixed value and then RD1.xaml is a Resource Dictionary.

    Below is the output:

    Resource Dictionary

    Code Output:

    PS: All ResourceDictionary Files should have Build Action as 'Resource' or 'Page'.

    Update :

    Finally I'm able to do this. Please use below method:

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

    OUTPUT:

    Without any try-catch & expected Exceptions and I think in more WPF(instead of converting everything to ResourceDictionary) way.

    0 讨论(0)
  • 2021-01-06 02:42

    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.

    0 讨论(0)
提交回复
热议问题