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.
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.
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.
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.
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 toResourceDictionary
) way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With