繁体   English   中英

ResourceDictionary.MergedDictionaries导致奇怪的错误

[英]ResourceDictionary.MergedDictionaries causes weird errors

在我的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>

元数据覆盖和基本元数据必须是相同类型或派生类型。

该解决方案可以成功构建并运行。

重新启动Visual Studio无法修复它。

剪切和粘贴<ResourceDictionary Source="..." />行会导致另一个错误,如注释中的此处所述: Value Cannot be Null. Parameter Name: item Value Cannot be Null. Parameter Name: item 重新启动Visual Studio将带回旧错误。

可悲的是,我还没有找到如何重现此错误的方法,我只能告诉您一些有关环境的信息:

  • Visual Studio 2015专业版14.0.25431.01更新3

尽管我对此表示怀疑,但这些都是与我的问题有关的,这里是我安装的插件:

  • Resharper Ultimate 2017.1.1
  • GitExtensions版本2.49.03

Sinatr的评论提示我阅读有关主题的更多信息。

ThemeInfo

在自定义控件库中,在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)
)]

参数

正如它在自动生成的注释中指出的那样,第一个参数是确定是否存在主题或特定于主题的资源字典。 第二个参数定义在哪里存在或在何处找到通用ResourceDictionaryGeneric.xaml )。

ResourceDictionaryLocation枚举

ResourceDictionaryLocation -Enumeration本身用于指定这些词典的位置。

ResourceDictionaryLocation.None

不存在主题词典。

ResourceDictionaryLocation.SourceAssembly

程序集中存在主题字典,用于定义主题的类型。 期望ResourceDictionary位于/Themes -Folder中。 稍后解释。

ResourceDictionaryLocation.ExternalAssembly

主题词典存在于定义主题类型的程序集之外的程序集中。

我不会解释它是如何工作的。

为什么/Themes

可悲的是我找不到太多的东西。 如果有人有更多信息,请分享。

您是否想过,如何应用无外观控件的样式?

如果创建了一个外观难看的控件,他将执行以下操作:

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)));
    }
}

简而言之,通过搜索Logical-Tree ,然后在Application的Resources中,最后在sth中,可以找到WPF中的资源。 他们称为System-Area (如果您知道更好的话,这是我的德语翻译)。

因此,取决于ThemeInfoMyControl可能在/Themes -Folder内部的ResourceDictionary具有其样式,例如。 /Themes/Generic.xaml 告诉 WPF的Ressources添加到System-Area这最终导致自动解决相应的样式。

/Themes/Generic.xaml内部的某个地方:

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

这就是为什么上述ThemeInfoAttribute要求Generic.xaml将位于/Themes -文件夹。 -而且,即使在我的情况下,即使该通用文件都没有使用System-Area Functionality,也会导致这些错误。 但是我无法找出原因。

资料来源:

暂无
暂无

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

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