Programmatically Merging a ResourceDictionary from Another Assembly
by Marc Melvin on Jan.27, 2010, under WPF
I recently had to find a way to merge a ResourceDictionary that is embedded in a class library into my application’s MergedDictionaries collection, so this is how I did it. I created the following extension method:
static public class ExtensionHelpers { static public void AddAssemblyResource(this Application app, string assemblyName, string path) { if (!UriParser.IsKnownScheme("pack")) UriParser.Register(new GenericUriParser(GenericUriParserOptions.GenericAuthority), "pack", -1); ResourceDictionary dict = new ResourceDictionary(); Uri uri = new Uri("/" + assemblyName + ";component/" + path, UriKind.Relative); dict.Source = uri; app.Resources.MergedDictionaries.Add(dict); } } |
Then, to add my resource, I simply call
App.Current.AddAssemblyResource("My.Assembly.Name", "Path/To/Resource.xaml"); |
No related posts.
1 comment for this entry:















January 28th, 2010 on 11:37 am
Nice and clean way to abstract out the “pack” and “component” keywords when dealing with resources in WPF. Those things create suckage, but this implementation hides it from the developer.