Question

When I include an assembly containing a ResourceDictionary using the following pack syntax:

"pack://application:,,,/WpfCore;component/ResourceDictionaries/ThemedControls.xaml"

It works as expected, but as soon as I add a code behind file to the XAML of the ResourceDictionary, the following error is thrown: “An error occurred while finding the resource dictionary”

The code behind is added to the XAML in the usual way:

< ResourceDictionary x:Class="com.mycompany.WpfCore.ResourceDictionaries.ThemedControls"
                    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">
</ResourceDictionary>

and looks like this:

namespace com.mycompany.WpfCore.ResourceDictionaries
{
    public partial class ThemedControls : ResourceDictionary
    {
        public ThemedControls ()
        {
            InitializeComponent();
        }
    }
}

Intuition tells me this is a namespace problem, but all the variations I've tried fail. What am I doing wrong or is this a limitation of WPF ResourceDictionaries?

Edit:

Seems the question detail was called out and found to be wanting.

The initial example had the namespace simplified. The default namespace for the WpfCore project is com.mycompany.WpfCore which I have now added into the code examples above.

The ThemedControls.xaml and ThemedControls.xaml.cs files are located in a subfolder called ResourceDictionaries within the WpfCore project folder. The resulting assembly is used as a referenced assembly in another solution and this is where the Pack URI is being used.

Edit 2:

After playing around with the build action for the xaml files (changing from page to resource and back again) things started working. Marking Sheridan's answer as correct.

Was it helpful?

Solution

I don't think that you have declared your ResourceDictionary quite correctly... the application name really should be in the namespace. This should work... at least it works for me:

<ResourceDictionary 
    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"
    x:Class="WpfCore.ResourceDictionaries.ThemedControls">
</ResourceDictionary>

Code behind:

namespace WpfCore.ResourceDictionaries
{
    public partial class ThemedControls : ResourceDictionary
    {
        public ThemedControls()
        {
            InitializeComponent();
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top