Unable to merge ResourceDictionary from a referenc

2019-03-30 08:35发布

问题:

I have two assemblies that each provide a common set of styles and resources that I want to include in my application. I'm using merged dictionaries in my App.xaml in order to load them, and at runtime they were just fine. Unfortunately, these resources won't load at design time, filling my Errors window with messages about unresolvable resources and giving me a UI that doesn't represent what will actually appear.

This is my App.xaml as it stands right now:

<Application x:Class="ClientDebug.App"
             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="/Dai.Common;component/Xaml/Common.xaml" />
                <ResourceDictionary Source="/Dai.DevExpress;component/Xaml/Styles.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

For both of these merged dictionaries, I get the following errors in my Errors window:

Error   11  An error occurred while finding the resource dictionary "/Dai.Common;component/Xaml/Common.xaml".   C:\DevProjects\Core Application\ClientDebug\App.xaml    12  17  ClientDebug
Error   12  An error occurred while finding the resource dictionary "/Dai.DevExpress;component/Xaml/Styles.xaml".   C:\DevProjects\Core Application\ClientDebug\App.xaml    13  17  ClientDebug

Which are obviously short on helpful information. Again, they load at runtime just as you'd expect, but none of the resources are available at design time.

回答1:

I have this every time I start a new WPF project or work with an old one ever since a few days ago. I started a question to ask for help on it, only for it to disappear on me on a test Solution! Seems Visual Studio's not very stable, who knew?

The core of the problem is that the Xaml designer isn't allowing you to make references to other Projects in your Solution from your Application Project only. The workaround is to move everything into a Library and have the Application do little more than hand off control to it in code. Inelegant and the loss of App.Xaml is very tough, but it's much better than waiting for Visual Studio to magically figure it all out.



回答2:

I precede the dictionaries that are from other namespaces with "pack://application:,,," like so:

        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/Wsi.Util;component/DataGrid/NameValueDataGridDictionary.xaml" />
            <ResourceDictionary Source="LayoutStylesDictionary.xaml" />
            <ResourceDictionary Source="pack://application:,,,/Wsi.Util;component/controls/ToolBarResources.xaml" />
            <ResourceDictionary Source="pack://application:,,,/Wsi.Common;component/view/themes/FilterStylesResourceDictionary.xaml" />
        </ResourceDictionary.MergedDictionaries>

HTH,

Janene



回答3:

I tried a similar merging in a project of mine and I seem to have it working perfectly. (or so, forgive me if its some settings of mine that I may have set long ago..) The only question I can ask is if the xamls are referring to anything within the parent project? I am assuming you have a reference to the parent project also (just cross-checking). Maybe the xaml file needs another xaml file too (controls, colors, fonts etc). (The merging that I tried was from MahApps.)

  <Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colours.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseDark.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
  </Application.Resources>


回答4:

I fixed this issue by signing the referenced project.

Problem was: My project was signed with a key file but the referenced project not.

To solve:

  • Load the project into the solution
  • Opened the project properties
  • Navigate to 'Signing'
  • Click 'Sign the assembly'
  • Select '<New...>' from the combo box
  • Enter a name, eg. 'keyfile' ('Protect my keyfile with a password' is optional)
  • Rebuild solution