c# - ResourceDictionary.MergedDictionaries导致奇怪的错误

标签 c# wpf visual-studio-2015 compiler-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>

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



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

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

剪切和粘贴<ResourceDictionary Source="..." />行会导致另一个错误,如注释中here所述: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在自定义控件库内部,在ThemeInfoAttribute内部自动创建了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)
    )]
    
    参量
    正如它在自动生成的注释中指出的那样,第一个参数用于确定是否存在主题或特定于主题的资源字典。
    第二个参数定义了是否存在通用ResourceDictionary(Generic.xaml)或在哪里找到。ResourceDictionaryLocation-枚举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. Explanation later.

    ResourceDictionaryLocation.ExternalAssembly

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


    我不会解释它是如何工作的。
    为什么/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可能在ResourceDictionary -Folder内部的/Themes中具有其样式。 /Themes/Generic.xaml。还有,即告诉WPF将资源添加到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>
    

    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.


    资料来源:
  • ThemeInfoAttribute Constructor
  • ResourceDictionaryLocation Enumeration
  • Windows Presentation Foundation-Das Umfassende Handbuch,Rheinwerk Computing版本4
  • 关于c# - ResourceDictionary.MergedDictionaries导致奇怪的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43887257/

    相关文章:

    c# - string.Format 与 string.Join

    c# - 在什么情况下我不应该在类中使用静态成员?

    wpf - ItemTemplate:ListBox 与 ItemsControl

    c# - WinForm插件系统

    javascript - CSHTML 中javascript 的断点?

    c++ - 无法使用 Microsoft CodeGen 使用 Visual Studio 2015 和 Clang 3.7 构建 Google Test

    c# - 从 C# 调用 VmWare PowerCLI

    wpf - 将数据上下文字符串属性绑定(bind)到 StaticResource 键

    c++ - VC++编译器升级2010->2015重新定义; 'constexpr' 说明符不匹配

    c# - Json字符串转C#对象