croxy
croxy

Reputation: 4168

Referencing the current assembly in xaml with anything other than its AssemblyShortName

I ran into a problem while developing a WPF application which is based on plugins using MEF.
Every plugin can have its own ResourceDictionary which includes different styles and colors. To let my main application (which loads all the plugins) know about all the different ResourceDictionaries I created a class for every dictionary and decorated the class with the [Export(typeof(ResourceDictionary))] attribute. In my main application I then import all exported ResourceDictionaries and add them to Application.Current.Resources.MergedDictionaries. So far everything is working fine.
The only thing which is bothering me is, that when referencing a resource in one of the views of the plugins I have to use DynamicResource in order to avoid VisualStudio to show an error message like:

The resource ... could not be resolved.

Even though at compiletime and runtime everything works fine.
To workround this issue of only using DynamicResource (because DynamicResource calls are more expensive than StaticResource calls) I added the ResourceDictionaries in every plugin-view I use them:

<UserControl.Resources>
   <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
         <ResourceDictionary Source="/resources/ColorDictionary.xaml" />
         <ResourceDictionary Source="/resources/StyleDictionary.xaml" />
      </ResourceDictionary.MergedDictionaries>
   </ResourceDictionary>
</UserControl.Resources>

Now because I reference them relative to the path of the plugin-assembly I get error messages in my main application:

It can't find the file "resources/StyleDictionary.xaml"

Which is understandable because my main application doesn't has such a file.
Now to avoid this error I found a solution which is instead of:

<ResourceDictionary Source="/resources/ColorDictionary.xaml" />

I hand over the AssemblyShortName of the current plugin-assembly like this:

<ResourceDictionary Source="/MyAssemblyShortName;component/resources/SymbolDictionary.xaml" />

When doing it like this no errors are given and all resources getting loaded fine. BUT what if the AssemblyShortName gets changed over time (the project is still at the beginning of its development). If so I have to find all this references and change them by hand.

TL;DR / Question:
Is there another way instead of using the AssemblyShortName to reference the current assembly as an absolute path in XAML. Because this:

<ResourceDictionary Source="/My.Namespace.PluginAssembly;component/resources/SymbolDictionary.xaml" />

gives me the error:

Assembly is not referenced by this project.

(Of course not because the "assembly" is "this project")

Upvotes: 2

Views: 970

Answers (1)

mm8
mm8

Reputation: 169270

Is there another way instead of using the AssemblyShortName to reference the current assembly as an absolute path in XAML.

No, I don't think so. The name of the assembly is part of the pack URI: https://msdn.microsoft.com/en-us/library/aa970069(v=vs.110).aspx.

If you don't want to find all references and modify the them manually when you change the name of the assembly you could either avoid changing the name of the assembly altogether or add the resource dictionaries programmatically:

Set up application resources from code

Then you could easily get the assembly name dynamically:

Getting assembly name

Upvotes: 1

Related Questions