Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to place XAML-only resources in a DLL (Reference) file to be used by WPF applications?

My WPF applications are reaching the level of complexity at which it becomes desirable to place some of the code and other resources inside a Reference file (*.dll).

I am sure I can figure out how to do this, by following the myriad applications out there.

In this particular case, however, the files are two, XAML:

enter image description here

Can somebody please provide an example on how to expose or publish the XAML resources? Are *.cs files required?

enter image description here

TIA

The XAML file outlined by @HighCore should look like this:

<Application x:Class="Application"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/ReferencedAssembly;component/Subfolder/ResourceFile.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

1 Answers

XAML ResourceDictionaries do not generate classes. I'm not sure why you would expect such thing.

Simply move the needed ResourceDictionary definitions to a separate assembly then use the Pack URI Syntax to merge these resources to your application's Resources in app.xaml:

<Application ....>
   <Application.Resources>
      <ResourceDictionary>
         <ResourceDictionary.MergedDictionaries>
             <ResourceDictionary Source="pack://application:,,,/ReferencedAssembly;component/Subfolder/ResourceFile.xaml"/> 
         </ResourceDictionary.MergedDictionaries>
      </ResourceDictionary>
   </Application.Resources>
</Application>
like image 156
Federico Berasategui Avatar answered Dec 07 '25 20:12

Federico Berasategui



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!