【问题标题】:DataBinding error数据绑定错误
【发布时间】:2011-10-04 21:24:29
【问题描述】:

我希望我的标题能更具描述性,但我无法理解下面的例外情况。

我最近发生了这种情况,结果证明是一个没有正确声明的 DependencyProperty,但我发现没有这个异常的帮助。

关于它的唯一另一件事是,当调用我的 GetHashCode 方法时会发生这种情况,这会触发一个方法,该方法通过反映对象的公共属性来派生散列。在这种情况下,该对象是从 ViewModelBase 类派生的,并且异常在它调用 Item 的属性上被命中,我猜它是 IDataErrorInfo 的一部分的索引器,但这只是一个猜测。

综上所述,我的直觉是,在输出任何有用的调试信息之前,它只是一个糟糕的 DataBinding。

有什么建议吗?

干杯,
浆果

System.Reflection.TargetParameterCountException was unhandled by user code
  Message=Parameter count mismatch.
  Source=mscorlib
  StackTrace:
       at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
       at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
       at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
       at Smack.Core.Lib.DomainSuperTypes.EntityImpl.BaseObject.GetHashCode() in C:\Users\Lord & Master\Documents\Projects\Smack\trunk\src\Core\DomainSuperTypes\EntityImpl\BaseObject.cs:line 51
       at Smack.Core.Lib.DomainSuperTypes.EntityImpl.ValueObject.GetHashCode() in C:\Users\Lord & Master\Documents\Projects\Smack\trunk\src\Core\DomainSuperTypes\EntityImpl\ValueObject.cs:line 68
       at System.Collections.Hashtable.get_Item(Object key)
       at System.ComponentModel.TypeDescriptor.NodeFor(Object instance, Boolean createDelegator)
       at System.ComponentModel.TypeDescriptor.GetDescriptor(Object component, Boolean noCustomTypeDesc)
       at System.ComponentModel.TypeDescriptor.GetPropertiesImpl(Object component, Attribute[] attributes, Boolean noCustomTypeDesc, Boolean noAttributes)
       at System.Windows.PropertyPath.ResolvePropertyName(String name, Object item, Type ownerType, Object context, Boolean throwOnError)
       at System.Windows.PropertyPath.ResolvePropertyName(Int32 level, Object item, Type ownerType, Object context)
       at MS.Internal.Data.PropertyPathWorker.GetInfo(Int32 k, Object item, SourceValueState& svs)
       at MS.Internal.Data.PropertyPathWorker.ReplaceItem(Int32 k, Object newO, Object parent)
       at MS.Internal.Data.PropertyPathWorker.UpdateSourceValueState(Int32 k, ICollectionView collectionView, Object newValue, Boolean isASubPropertyChange)
       at MS.Internal.Data.ClrBindingWorker.AttachDataItem()
       at System.Windows.Data.BindingExpression.Activate(Object item)
       at System.Windows.Data.BindingExpression.AttachToContext(AttachAttempt attempt)
       at System.Windows.Data.BindingExpression.AttachOverride(DependencyObject target, DependencyProperty dp)
       at System.Windows.Data.BindingExpressionBase.Attach(DependencyObject target, DependencyProperty dp)
       at System.Windows.StyleHelper.GetInstanceValue(UncommonField`1 dataField, DependencyObject container, FrameworkElement feChild, FrameworkContentElement fceChild, Int32 childIndex, DependencyProperty dp, Int32 i, EffectiveValueEntry& entry)
       at System.Windows.StyleHelper.GetChildValueHelper(UncommonField`1 dataField, ItemStructList`1& valueLookupList, DependencyProperty dp, DependencyObject container, FrameworkObject child, Int32 childIndex, Boolean styleLookup, EffectiveValueEntry& entry, ValueLookupType& sourceType, FrameworkElementFactory templateRoot)
       at System.Windows.StyleHelper.GetChildValue(UncommonField`1 dataField, DependencyObject container, Int32 childIndex, FrameworkObject child, DependencyProperty dp, FrugalStructList`1& childRecordFromChildIndex, EffectiveValueEntry& entry, ValueLookupType& sourceType, FrameworkElementFactory templateRoot)
       at System.Windows.StyleHelper.GetValueFromTemplatedParent(DependencyObject container, Int32 childIndex, FrameworkObject child, DependencyProperty dp, FrugalStructList`1& childRecordFromChildIndex, FrameworkElementFactory templateRoot, EffectiveValueEntry& entry)
       at System.Windows.StyleHelper.ApplyTemplatedParentValue(DependencyObject container, FrameworkObject child, Int32 childIndex, FrugalStructList`1& childRecordFromChildIndex, DependencyProperty dp, FrameworkElementFactory templateRoot)
       at System.Windows.StyleHelper.InvalidatePropertiesOnTemplateNode(DependencyObject container, FrameworkObject child, Int32 childIndex, FrugalStructList`1& childRecordFromChildIndex, Boolean isDetach, FrameworkElementFactory templateRoot)
       at System.Windows.FrameworkTemplate.InvalidatePropertiesOnTemplate(DependencyObject container, Object currentObject)
       at System.Windows.FrameworkTemplate.<>c__DisplayClass6.<LoadOptimizedTemplateContent>b__3(Object sender, XamlObjectEventArgs args)
       at System.Xaml.XamlObjectWriter.Logic_CreateAndAssignToParentStart(ObjectWriterContext ctx)
       at System.Xaml.XamlObjectWriter.WriteStartMember(XamlMember property)
       at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader, XamlObjectWriter currentWriter)
  InnerException: 

更新

下面是捕获异常的那一行:

var value = property.GetValue(this, null);

这是该方法的一部分

    public override int GetHashCode() {
        unchecked {
            IEnumerable<PropertyInfo> signatureProperties = GetSignatureProperties().ToArray();

            // It's possible for two objects to return the same hash code based on 
            // identically valued properties, even if they're of two different types, 
            // so we include the object's type in the hash calculation
            var hashCode = GetType().GetHashCode();

            foreach (var property in signatureProperties) {
                var value = property.GetValue(this, null);

                if (value != null)
                    hashCode = (hashCode * HASH_MULTIPLIER) ^ value.GetHashCode();
            }

            if (signatureProperties.Any())
                return hashCode;

            // If no properties were flagged as being part of the signature of the object,
            // then simply return the hashcode of the base object as the hashcode.
            return base.GetHashCode();
        }
    }

【问题讨论】:

  • 你能添加一些代码吗?异常发生在哪里?
  • 你能转储signatureProperties的内容吗?是否有这些属性索引器,即prop[i]
  • @mellamokb 是的,我很确定索引器是 IDataError 接口的一部分。我不知道如何转储属性

标签: c# wpf data-binding reflection datatemplate


【解决方案1】:

当您调用property.GetValue( 时,第二个参数用于指定索引器属性的索引值。如果一个属性是一个索引器并且你传递 null,那么你将得到一个异常。您需要确定哪些属性是索引器并有一个值作为第二个参数传入。

要添加引用,根据documentation(在异常部分下),在以下情况下会抛出TargetParameterCountException

索引中的参数个数与参数个数不匹配 索引属性采用的参数。

这听起来像你的确切场景。

希望这会有所帮助!

【讨论】:

  • 是的,这对我有很大帮助,因为该索引器与签名属性无关 我最近所做的更改产生了这样做的意外后果,现在我撤消了它,我可以查看我所有的数据绑定错误(这实际上比没有看到要好得多!)
猜你喜欢
  • 2018-03-27
  • 2018-05-15
  • 2011-02-24
  • 2017-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多