C#WPF XAML项目已添加。键入字典:添加'EqualityConverter'键:'EqualityConverter'

时间:2015-08-19 21:11:14

标签: c# wpf xaml resourcedictionary

我收到以下错误:

System.Windows.Markup.XamlParseException occurred
  HResult=-2146233087
  Message='Set property 'System.Windows.ResourceDictionary.DeferrableContent' threw an exception.' Line number '9' and line position '6'.
  Source=PresentationFramework
  LineNumber=9
  LinePosition=6
  StackTrace:
       at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
  InnerException: System.ArgumentException
       HResult=-2147024809
       Message=Item has already been added. Key in dictionary: 'EqualityConverter'  Key being added: 'EqualityConverter'
       Source=mscorlib
       StackTrace:
            at System.Collections.Hashtable.Insert(Object key, Object nvalue, Boolean add)
            at System.Collections.Hashtable.Add(Object key, Object value)
            at System.Windows.ResourceDictionary.SetKeys(IList`1 keyCollection, IServiceProvider serviceProvider)
            at System.Windows.ResourceDictionary.SetDeferrableContent(DeferrableContent deferrableContent)
            at System.Windows.Baml2006.WpfSharedBamlSchemaContext.<Create_BamlProperty_ResourceDictionary_DeferrableContent>b__168(Object target, Object value)
            at System.Windows.Baml2006.WpfKnownMemberInvoker.SetValue(Object instance, Object value)
            at MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(XamlMember member, Object obj, Object value)
            at MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(Object inst, XamlMember property, Object value)
       InnerException: 

我有 C#类库 ,名为 ABC ,其中包含一些WPF(XAML)窗口。 我还有另一个 C#Windows应用程序 项目,名为 ABC设置,其 App.xaml StartupUri 使用正确的Pack URI格式指向类库中的一个窗口。窗口类位于类库中,因为它需要可以从另一个 Windows应用程序 访问,名为 ABC客户端

<Application x:Class="CLASS_HERE"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="pack://application:,,,/ABC;component/ABCWindow.xaml">
    <Application.Resources>

    </Application.Resources>
</Application>

ABCWindow.xaml 中,我有一个完整的窗口,我记得几个月前工作(从 ABC设置项目启动时)。 从ABC客户端启动窗口时,没有运行时错误!

在Window的XAML代码的顶部,我有一些转换器定义:

<Window.Resources>
    <ui:EqualityConverter x:Key="EqualityConverter" />
    <ui:VisibilityConverter x:Key="VisibilityConverter" />
</Window.Resources>

如果我更换了转换器的顺序,那么首先出现 VisibilityConverter ,然后错误消息显示 VisibilityConverter 是问题所在。

我能够通过将转换器包装在 ResourceDictionary 中来摆脱这个特定错误,但后来又遇到了另一个错误:

System.Windows.Markup.XamlParseException occurred
  HResult=-2146233087
  Message='Could not register named object. Cannot register duplicate name 'addButton' in this scope.' Line number '65' and line position '26'.
  Source=PresentationFramework
  LineNumber=65
  LinePosition=26
  StackTrace:
       at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
  InnerException: System.ArgumentException
       HResult=-2147024809
       Message=Cannot register duplicate name 'addButton' in this scope.
       Source=System.Xaml
       StackTrace:
            at System.Xaml.NameScope.RegisterName(String name, Object scopedElement)
            at System.Xaml.XamlObjectWriter.RegisterName(ObjectWriterContext ctx, String name, Object inst, XamlType xamlType, INameScope nameScope, INameScope parentNameScope, Boolean isRoot)
       InnerException: 

我在Window的XAML代码中有下面的代码:

<Button x:Name="addButton" Content="Add" Click="addButton_Click" />

似乎XAML解析器/框架/无论认为我有重复的密钥和对象名称,但我肯定没有。 就像我上面说的那样,从 ABC客户端启动时,此窗口运行正常。

我在MSDN论坛和随机博客上搜索过。我已经清理并重建了我的整个解决方案。我重新启动了Visual Studio,甚至手动删除了所有临时Visual Studio文件。

我已确保所有文件都具有正确的构建类型:

ABCWindow.xaml 页面

App.xaml ApplicationDefinition

我的猜测是 ABC设置项目和 StartupUri 存在问题,无论内部机制将所有这些连接在一起。

任何人都有任何关于出了什么问题的建议? 谢谢你的阅读。

0 个答案:

没有答案