Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

wpf

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.

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.

like image 853
ionoy Avatar asked Apr 15 '16 06:04

ionoy


People also ask

What is ResourceDictionary?

A resource dictionary is a repository for XAML resources, such as styles, that your app uses. You define the resources in XAML and can then retrieve them in XAML using the {StaticResource} markup extension and {ThemeResource} markup extension s. You can also access resources with code, but that is less common.

How do you reference a resource dictionary?

You can reference a resource throughout an app or from any XAML page within it. You can define your resources using a ResourceDictionary element from the Windows Runtime XAML. Then, you can reference your resources by using a StaticResource markup extension or ThemeResource markup extension.


2 Answers

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.

like image 151
StepUp Avatar answered Sep 30 '22 06:09

StepUp


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

Resource Dictionary

Code Output:

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:

RD

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

like image 34
Kylo Ren Avatar answered Sep 30 '22 04:09

Kylo Ren