1

I have made several ResourceDictionaries for our applications team to use with their future applications. I have deployed the contents of the class library project containing these dictionaries to a .dll file and would like to be able to use the dictionaries by adding a reference to the .dll file in a new WPF solution where I hope to make a new application.

The Class Library in my example is called "NWF_Class_Library.dll" and is saved in the same folder in windows explorer as the MainWindow.xaml file. Is it possible to retrieve the resource dictionaries from within it?

enter image description here

I have read articles about the best way for an organisation to arrange their xaml resources, so it seems it must be possible, but all I find is ways to use the "//pack:application:..." syntax to reference xaml within the same solution as the wpf application. Here is a snippet of code, with the Source blank because nothing I have written has worked!

We had hoped that we could add the standard configurations as well as our more normal useful methods etc to a file that can be deployed with applications.

<Window x:Class="dll_ref_included.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <ResourceDictionary Source=""/>
    </Window.Resources>
    <Grid>
        <Button Style="{StaticResource myButton}">This</Button>
    </Grid>
</Window>
mm8
  • 163,881
  • 10
  • 57
  • 88
High Plains Grifter
  • 1,357
  • 1
  • 12
  • 36
  • 1
    It is posible with _"//pack:application:..."_ syntax. You may provide a strong assembly name and a resource name, that you wish to use. As far as I know, that this assembly must be loaded in your current AppDomain (added to reference of your project, or manually). – vasily.sib Aug 10 '18 at 08:23

1 Answers1

3

Try this:

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

...where "NWF_Class_Library" is the name of the referenced assembly and "Dictionary1.xaml" is the name of the ResourceDictionary that is defined in this project.

You can refer to the documentation for more information about pack URIs and how to use them.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • Unbelievable! I had already tried this syntax and get an error in the designer saying `An error occurred while finding the resource dictionary "pack://application:,,,/NWF_Class_Library;component/WPF Resources/Buttons.xaml".` but when I ignore the error and run it anyway (which I had not done before), I am presented with a perfect button! What the hell?! – High Plains Grifter Aug 10 '18 at 09:01
  • The designer is a bit buggy and can't handle all scenarios. It's the runtime behaviour that counts. You may want to disable the designer altogether: https://stackoverflow.com/questions/10481645/disable-xaml-ui-designer – mm8 Aug 10 '18 at 09:09
  • Man, so much time wasted for the sake of that! I am near the beginning of my xaml learning and was so sure I had the syntax right; just never actually pressed F5 when it was giving an error! I'll know to take the designer errors with more seasoning next time - I'll see if Blend does the same thing; it would be a pity not to be able to see the forms in the moment of designing them... – High Plains Grifter Aug 10 '18 at 09:15
  • Microsoft Blend shows the preview properly - I'll use that. Man! So much wastage! Thanks. – High Plains Grifter Aug 10 '18 at 09:18