简体   繁体   中英

ResourceDictionary.MergedDictionaries causes weird errors

Inside my WPF Application I am including a ResourceDictionary from another Project.

<Application x:Class="namespace.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <!-- This Line causes an Error -->
                <ResourceDictionary Source="pack://application:,,,/Commons;Component/Generic.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

Metadata override and base metadata must be of the same type or derived type.

The solution builds successful and runs.

Restarting Visual Studio doesn't fix it.

Cut and Paste the <ResourceDictionary Source="..." /> line causes another error as explained here in the Comments: Value Cannot be Null. Parameter Name: item Value Cannot be Null. Parameter Name: item . Restarting Visual Studio will then bring back the old error.

Sadly I haven't found out how to reproduce this error, I can only tell you something more about the environment im using:

  • Visual Studio 2015 Professional, Version 14.0.25431.01 Update 3

And allthough I doubt, those are associated with my problem, here my installed Plugins:

  • Resharper Ultimate 2017.1.1
  • GitExtensions Version 2.49.03

Sinatr 's comment hinted me to read more about theming.

ThemeInfo

Inside of a Custom Control Library theres automatically created a ThemeInfoAttribute inside AssemblyInfo.cs

[assembly:ThemeInfo(
 ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
                          //(used if a resource is not found in the page, 
                          // or application resource dictionaries)
 ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
                                   //(used if a resource is not found in the page, 
                                   // app, or any theme specific resource dictionaries)
)]

Parameters

As it states in the autogenerated comments, the first parameter is to determine wheter there exist or where to find Theme specific resource dictionaries. The second parameter defines wheter there exist or where to find the generic ResourceDictionary ( Generic.xaml ).

ResourceDictionaryLocation -Enumeration

The ResourceDictionaryLocation -Enumeration itself is used to specify the location of those dictionaries.

ResourceDictionaryLocation.None

No theme dictionaries exist.

ResourceDictionaryLocation.SourceAssembly

Theme dictionaries exist in the assembly that defines the types being themed. This expects the ResourceDictionary to be located in a /Themes -Folder. Explanation later.

ResourceDictionaryLocation.ExternalAssembly

Theme dictionaries exist in assemblies external to the one defining the types being themed.

I am not going to explain how this works.

Why /Themes -Folder

Sadly I couldn't find too much about this. If someone has some more info please share.

Have you ever wondered, how styles of a lookless control are being applied?

If one created a lookless Control, he did as follows:

public class MyControl : ControlTemplate
{
    static MyControl() 
    {
        // This tells WPF to search for a Style for this type
        DefaultStyleKeyProperty.OverrideMetadata(typeof(MyControl)), 
            new FrameworkPropertyMetadata(typeof(MyControl)));
    }
}

In short, Ressources in WPF are located, by searching up the Logical-Tree , then inside Application 's Resources and finally inside sth. they call System-Area (this is my translation from German, if you know a better one pls tell).

So depending on ThemeInfo , MyControl propably had its Style inside a ResourceDictionary inside the /Themes -Folder, eg. /Themes/Generic.xaml . And that tells WPF to add the Ressources to the System-Area which finally results in automatically resolving the appropriate style.

Somewhere inside /Themes/Generic.xaml :

<Style TargetType="{x:Type MyControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type MyControl}">
            ..
            </ControlTemplate/>
        </Setter.Value>
    </Setter>
</Style>

That's why the above ThemeInfoAttribute requires Generic.xaml to be located in a /Themes -Folder. - And somehow, even if in my case the System-Area -Functionality isn't even used for this generic file, this causes those errors. But I wasn't able to find out why.

Sources:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM