【问题标题】:XamlParseException: Attribute in custom control missing, but it's defined!XamlParseException:自定义控件中的属性缺失,但已定义!
【发布时间】:2008-10-23 09:37:31
【问题描述】:

我的自定义控件有时会出现以下异常:

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) 

知道控件出了什么问题,或者我可以做些什么来找到这些异常的来源?正如我所说,这些问题仅在控件实例化几十次时发生。

【问题讨论】:

  • 既然这个问题现在很流行,我想跟进:在这种情况下,我们从来没有发现问题。自从我从事此工作以来已经有一段时间了,但我认为一个团队成员通过在循环中调用 XAML 根节点的对象的构造函数来“解决”它,重复直到没有抛出异常。

标签: .net silverlight silverlight-2.0


【解决方案1】:

我也遇到过这个问题。我对此没有很好的解释(似乎是 XAML 解析错误),但我已通过向 XAML 添加以下代码来修复它:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      我认为我们看到了同样的问题。我只是更频繁地得到 invalidcast 异常。当通过对内容的盲目评论,我可以抑制无效转换时,我有时确实会得到一个属性的属性缺失错误,事实上,那里的属性。你找到解决办法了吗?

      顺便说一句:重现它的简单方法是加载有问题的应用程序并反复按 F5 以刷新页面。你最终会得到它。

      我还看到了来自 DependencyObject.get_NativeObject() 的间歇性 InvalidOperationException。

      在此处查看我的 SO 问题: Intermittent InavlidCastException in a UserControl.InitializeComponent()

      在此处查看我的 silverlight.net 错误报告: http://silverlight.net/forums/t/67732.aspx

      【讨论】:

        【解决方案4】:

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

        【讨论】:

        • 肯定有例外,请参阅我对原始帖子的评论。但这几乎是随机的,正如您可以从我们对该问题的“解决方案”中看出的那样。不过感谢您提供的信息。
        猜你喜欢
        • 2014-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-19
        • 1970-01-01
        • 2012-02-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多