首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DataBinding错误

DataBinding错误
EN

Stack Overflow用户
提问于 2011-10-05 05:24:29
回答 1查看 2K关注 0票数 2

我希望我的标题能更具描述性,但我在理解下面的例外时遇到了困难。

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

我可以添加的另一件事是,当我的GetHashCode方法被调用时,它就会发生,这会触发一个方法,该方法通过反映对象的公共属性来派生散列。在本例中,对象是从ViewModelBase类派生的,异常命中它称为Item的属性,我猜这是IDataErrorInfo的indexor部分,但这只是一个猜测。

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

有什么建议吗?

干杯,

Berryl

代码语言:javascript
复制
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: 

更新

下面是捕获异常的行:

代码语言:javascript
复制
var value = property.GetValue(this, null);

,它是此方法的一部分。

代码语言:javascript
复制
    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();
        }
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-10-05 08:20:11

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

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

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

这听起来就像你的场景。

希望这能有所帮助!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7654215

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档