2008-10-23 74 views
0

有时得到我的自定义控件以下异常:XamlParseException:自定义控件中的属性缺失,但已定义!

XamlParseException occurredUnknown attribute Points in element SectionClickableArea [Line: 10 Position 16]

堆栈跟踪:

{System.Windows.Markup.XamlParseException: Unknown attribute Points on element SectionClickableArea. [Line: 10 Position: 16] 
    at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator) 
    at SomeMainDialog.InitializeComponent() 
    at SomeMainDialog..ctor()} 

元素声明,其中发生这种情况看起来像这样(的这里引用的事件处理程序当然是定义的):

<l:SectionClickableArea x:Name="SomeButton" 
    Points="528,350, 508,265, 520,195, 515,190, 517,165, 530,120, 555,75, 570,61, 580,60, 600,66, 615,80, 617,335, 588,395, 550,385, 540,390, 525,393, 520,385" 
    Click="SomeButton_Click"/> 

这是SectionClickableArea代码的一部分:

public partial class SectionClickableArea : Button { 

    public static readonly DependencyProperty PointsProperty 
     = DependencyProperty.Register("Points", typeof(PointCollection), typeof(SectionClickableArea), 
      new PropertyMetadata((s, e) => { 
       SectionClickableArea area = (SectionClickableArea) s; 
       area.areaInfo.Points = (PointCollection) e.NewValue; 
       area.UpdateLabelPosition(); 
      })); 
    public PointCollection Points { 
     get { return (PointCollection) GetValue(PointsProperty); } 
     set { SetValue(PointsProperty, value); } 
    } 

我使用这个控制类似的多边形形状的按钮。所以我从按钮继承。我有类似的问题(E_AG_BAD_PROPERTY_VALUE在另一个DependencyProperty类型的字符串,根据给定的行和列等)与这种控制数周,但我绝对不知道为什么。


另一个例外对于同一控制今天上午发生了其他用户(从日志截断并从德国翻译):

Type: System.InvalidCastException Message: The object of type System.Windows.Controls.ContentControl could not be converted to type [...]SectionClickableArea. at SomeOtherMainDialog.InitializeComponent() 
    at SomeOtherMainDialog..ctor() 

内部异常:

Type: System.Exception Message: An HRESULT E_FAIL error was returned when calling COM component at MS.Internal.XcpImports.CheckHResult(UInt32 hr) 
    at MS.Internal.XcpImports.SetValue(INativeCoreTypeWrapper obj, DependencyProperty property, DependencyObject doh) 
    at MS.Internal.XcpImports.SetValue(INativeCoreTypeWrapper doh, DependencyProperty property, Object obj) 
    at System.Windows.DependencyObject.SetObjectValueToCore(DependencyProperty dp, Object value) 
    at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value, Boolean allowReadOnlySet, Boolean isSetByStyle, Boolean isSetByBuiltInStyle) 
    at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value) 
    at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value) 
    at System.Windows.Controls.Control.set_DefaultStyleKey(Object value) 
    at System.Windows.Controls.ContentControl..ctor() 
    at System.Windows.CoreTypes.GetCoreWrapper(Int32 typeId) 
    at MS.Internal.ManagedPeerTable.EnsureManagedPeer(IntPtr unmanagedPointer, Int32 typeIndex, Type type, Boolean preserveManagedObjectReference) 
    at MS.Internal.ManagedPeerTable.EnsureManagedPeer(IntPtr unmanagedPointer, Int32 typeIndex, Type type) 
    at MS.Internal.ManagedPeerTable.GetManagedPeer(IntPtr nativeObject) 
    at MS.Internal.FrameworkCallbacks.SetPropertyAttribute(IntPtr nativeTarget, String attrName, String attrValue, String attachedDPOwnerNamespace, String attachedDPOwnerAssembly) 

任何想法是什么控制权是否错误,或者我能做些什么来找到这些例外的来源?正如我所说的,这些问题只发生在控制实例化的每几十次。

+1

由于这是一个受欢迎的问题,我想跟进:在这种情况下,我们从未发现问题。自从我开始工作已经有一段时间了,但我认为一个团队成员通过在循环中调用XAML根节点的对象的构造函数来“解决”它,直到没有抛出异常为止。 – 2010-09-19 11:22:34

回答

0

我不是XAML专家,但是你缺少点上的命名空间(例如l:Points)吗?

0

我认为我们看到了同样的问题。我只是更频繁地得到invalidcast异常。当通过盲目评论的东西,我可以抑制无效播放,我有时会得到属性缺失的错误,实际上,这是一个属性。你找到了解决方案吗?

顺便说一句:简单的方法来重新载入它只是加载有问题的应用程序,并重复点击F5刷新页面。你会最终得到它。

我也看到了从DependencyObject.get_NativeObject()通过的间歇性InvalidOperationException。

看到我的SO问题在这里: Intermittent InavlidCastException in a UserControl.InitializeComponent()

见我silverlight.net错误报告在这里: http://silverlight.net/forums/t/67732.aspx

1

我也遇到了这个问题。我没有为一个很好的解释(似乎是XAML解析错误),但我已经通过添加以下代码来XAML固定它:

<UserControl.Resources> 
    <customNamespace:InheritedControl x:Name="dummyInstance"/> 
</UserControl.Resources> 
0

我有同样的问题。在我的情况下,错误发生在其中一个成员上。成员构造函数抛出异常。
我通过在InitializeComponent()方法之前放置断点并在调试模式下将F11按入Step Into并找到错误来解决此问题。
希望这会有所帮助。
谢谢。

+0

肯定有例外,请参阅我对原文的评论。但是,这几乎是随机的,正如你可以通过我们的“解决方案”所告诉的那样。谢谢你的信息。 – 2010-12-02 11:47:29