简体   繁体   English

ResourceDictionary.MergedDictionaries导致奇怪的错误

[英]ResourceDictionary.MergedDictionaries causes weird errors

Inside my WPF Application I am including a ResourceDictionary from another Project. 在我的WPF应用程序内部,我包括另一个项目的ResourceDictionary。

<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. 重新启动Visual Studio无法修复它。

Cut and Paste the <ResourceDictionary Source="..." /> line causes another error as explained here in the Comments: Value Cannot be Null. Parameter Name: item 剪切和粘贴<ResourceDictionary Source="..." />行会导致另一个错误,如注释中的此处所述: Value Cannot be Null. Parameter Name: item Value Cannot be Null. Parameter Name: item . Value Cannot be Null. Parameter Name: item Restarting Visual Studio will then bring back the old error. 重新启动Visual Studio将带回旧错误。

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 Visual Studio 2015专业版14.0.25431.01更新3

And allthough I doubt, those are associated with my problem, here my installed Plugins: 尽管我对此表示怀疑,但这些都是与我的问题有关的,这里是我安装的插件:

  • Resharper Ultimate 2017.1.1 Resharper Ultimate 2017.1.1
  • GitExtensions Version 2.49.03 GitExtensions版本2.49.03

Sinatr 's comment hinted me to read more about theming. Sinatr的评论提示我阅读有关主题的更多信息。

ThemeInfo

Inside of a Custom Control Library theres automatically created a ThemeInfoAttribute inside AssemblyInfo.cs 在自定义控件库中,在AssemblyInfo.cs自动创建了ThemeInfoAttribute

[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 ). 第二个参数定义在哪里存在或在何处找到通用ResourceDictionaryGeneric.xaml )。

ResourceDictionaryLocation -Enumeration ResourceDictionaryLocation枚举

The ResourceDictionaryLocation -Enumeration itself is used to specify the location of those dictionaries. ResourceDictionaryLocation -Enumeration本身用于指定这些词典的位置。

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. 期望ResourceDictionary位于/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 为什么/Themes

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. 简而言之,通过搜索Logical-Tree ,然后在Application的Resources中,最后在sth中,可以找到WPF中的资源。 they call System-Area (this is my translation from German, if you know a better one pls tell). 他们称为System-Area (如果您知道更好的话,这是我的德语翻译)。

So depending on ThemeInfo , MyControl propably had its Style inside a ResourceDictionary inside the /Themes -Folder, eg. 因此,取决于ThemeInfoMyControl可能在/Themes -Folder内部的ResourceDictionary具有其样式,例如。 /Themes/Generic.xaml . /Themes/Generic.xaml And that tells WPF to add the Ressources to the System-Area which finally results in automatically resolving the appropriate style. 告诉 WPF的Ressources添加到System-Area这最终导致自动解决相应的样式。

Somewhere inside /Themes/Generic.xaml : /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. 这就是为什么上述ThemeInfoAttribute要求Generic.xaml将位于/Themes -文件夹。 - And somehow, even if in my case the System-Area -Functionality isn't even used for this generic file, this causes those errors. -而且,即使在我的情况下,即使该通用文件都没有使用System-Area Functionality,也会导致这些错误。 But I wasn't able to find out why. 但是我无法找出原因。

Sources: 资料来源:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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