简体   繁体   中英

Using (merged) Resource Dictionary in C# Class Library Project

How can I use a (merged) WPF Resource Dictionary in a C# class library project?

Here is what I did:

In my C# class library project I have a file Dictionary1.xaml like that:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                >
    <Style x:Key="PluginFrameBorderStyle">
    ...
    </Style>
</ResourceDictionary>

Then, I have a UserControl file UserControl1.xaml where I try to use the Dictionary like that:

<UserControl x:Class="EditorPackageA.BackboneMemberB1Editor.BackboneMemberB1Editor"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         xmlns:local="clr-namespace:EditorPackageA.EditorBase"
         xmlns:prism="http://www.codeplex.com/prism" d:DesignWidth="690.4" d:DesignHeight="460.12">
<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Dictionary1.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>
...
</UserControl>

The project compiles but at runtime I get the error:

在此输入图像描述

with the exception detail:

在此输入图像描述

The same approach works when applied within a WPF project rather than a Class Library project .

What might be the solution here?

Important Addendum:

During design-time I see the effect of the used style that is embedded via the ResourceDictionary, hence the URI of the style and the dictionary must be correct!?

Try to use so called pack URI. I think that you have to explicitly specify where the resource dictionary is located.

<ResourceDictionary Source="pack://application:,,,/TheNameOfClassLibrary;component/Dictionary1.xaml"/>

In the case of a WPF project your approach works because WPF engine by default looks for resource in the assembly being executed (in exe).

You probably need to merge your library's resource dictionary in your application resources. You need to edit your App.xaml file and add something like:

<Application.Resources>
<ResourceDictionary>
  <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="pack://application:,,,/YourAssembly;component/Dictionary1.xaml" />
      </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
</Application.Resources>

You should refer/link the ResourceDictionary xml file to the Source attribute with assembly and component name.
Use relative path as follows:

<ResourceDictionary Source="../Dictionary1.xaml" /> 

If it is not working, then try PACK URL

<ResourceDictionary Source="pack://application:,,,/Your.Base.AssemblyName;component/DictionaryFolder/Dictionary1.xaml" />

Hope it helps you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM