Search code examples
wpfxamldllresourcedictionary

WPF DLL builds correctly but causes errors when used


I'm in Visual Studio 2017, using .Net 4.6.1

I have made a library (in Project A) of WPF styles, controls, converters etc for use when building applications. The idea is that if there is a corporate branding change, we can republish the library and all the applications referencing it will be re-branded.

Project A has no errors or warnings and builds correctly to make a .dll file.

When I come to reference the .DLL in Project B however, the xaml that references some of the .DLL components gives the Loading designer... You can continue working while the designer is loading in the background message forever. When I try to start Project B, It tells me it is in break mode, with a System.StackOverflowException and that's that.

I have tried adding and removing lines in the app.xaml file:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/WPFLibrary;component/Styles/Colours.xaml"/>
            <ResourceDictionary Source="pack://application:,,,/WPFLibrary;component/Styles/Brushes.xaml"/>
            <ResourceDictionary Source="pack://application:,,,/WPFLibrary;component/Styles/Text.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

and doing so gives me a hint, perhaps in that only some references (or maybe having too many references?) cause the designer to fail, but I'm having a hell of a time trying to find what's going on.

Do you have any idea how I might go about debugging this problem, given that Project A, where the problem appears to be, builds correctly? For instance any tips or tricks or extra tools that might identify problems at design time?

Alternatively, any advice on how to check everything in Project B is as it should be would be welcome. I have checked all the references, namespaces and resource dictionaries and confident that the syntax is correct, but maybe there's a gotcha that I could have missed?


Solution

  • So this, it turns out is one of those annoying problems which you leave for a weekend and when you return it has changed its look. I came back and could no longer get the stack overflow error.

    The error reported was a failure of the UserControl in Project A to find one of the resources built into the same project. I was able to fix this by changing the references in the Merged dictionary from standard to Pack URI format (the local path references only work when viewed in the project itself - they fail in the DLL). From this:

    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Styles/Buttons.xaml"/>
                <ResourceDictionary Source="/Styles/Gradients.xaml"/>
                <ResourceDictionary Source="/Styles/Text.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>
    

    to this:

    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/WPFLibrary;component/Styles/Buttons.xaml"/>
                <ResourceDictionary Source="pack://application:,,,/WPFLibrary;component/Styles/Gradients.xaml"/>
                <ResourceDictionary Source="pack://application:,,,/WPFLibrary;component/Styles/Text.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>
    

    From this I learnt - Local references only work locally in a DLL between resource dictionaries; if you wish to use a UserControl from the DLL, you have to use the pack format. In future I will use Pack URIs everywhere that they do not cause a syntax error!