Nerijus Pocevičius
Nerijus Pocevičius

Reputation: 35

Use style from external assembly, which is in another solution

I have a solution:
Image
In this solution I have a style which I want to use in another project, the style is in resource dictionary format. Note: This solution contains WPF class library.

In another solution I have a project, the solution explorer looks like this:
Image

Now, I want to reference the CustomWindow.xaml resource dictionary in my App.xaml, so I could use it in my MainWindow.xaml.

<ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="???"/>
    <ResourceDictionary.MergedDictionaries>
<ResourceDictionary>

How to do so?

The solution was:

<ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="pack://application:,,,/CustomWindow;component/Styles/CustomWindow.xaml"/>
    <ResourceDictionary.MergedDictionaries>
<ResourceDictionary>

Now, whenever I add the Style to my window from that DLL like this:

Style="{DynamicResource CustomWindowStyle}"

It shows wiggly lines on the code, and whenever I put a cursor on it, it shows an error: "Object reference not set to an instance of an object"
Note: It compiles, but the designer won't shot how it really looks like.

Upvotes: 1

Views: 1012

Answers (1)

Ivan Vargas
Ivan Vargas

Reputation: 703

Use pack URI: https://msdn.microsoft.com/en-us/library/aa970069(v=vs.110).aspx. In your case, it should be something like

<ResourceDictionary>
   <ResourceDictionary.MergedDictionaries>
       <ResourceDictionary Source="pack://application:,,,/<assembly name>;component/Styles/CustomWindow.xaml"/>
   <ResourceDictionary.MergedDictionaries>

If you are going to share resources, you might want to move the resources to another assembly.

Upvotes: 1

Related Questions