XamlParseException:缺少自定义控件中的属性,但已定义!

时间:2008-10-23 09:37:31

标签: .net silverlight silverlight-2.0

有时为我的自定义控件获取以下异常:

XamlParseException occurred Unknown 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) 

任何想法控件有什么问题,或者我可以做些什么来找到这些例外的来源?正如我所说,这些问题只发生在控制实例化的几十次。

4 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

我不是XAML专家,但你错过了Point上的命名空间(例如l:Points)?

答案 2 :(得分:0)

我认为我们遇到了同样的问题。我更频繁地得到无效广播例外。当通过盲目评论的东西,我可以抑制无效广播时,我确实有时会得到属性缺失错误的属性,实际上就是那里。你找到了解决方案吗?

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

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

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

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

答案 3 :(得分:0)

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