XamlParseException: Attribute in custom control mi

2019-02-20 23:48发布

I sometimes get the following exception for a custom control of mine:

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

The stack trace:

{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()}

The element declaration where this happens looks like this (the event handler referenced here is defined, of course):

<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"/>

This is part of the code of 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); }
    }

I use this control for something like a polygon-shaped button. Therefore I'm inheriting from button. I've had similar problems (E_AG_BAD_PROPERTY_VALUE on another DependencyProperty of type string, according to the line and column given, etc) with this control for weeks, but I have absolutely no idea why.


Another exception for the same control occurred this morning for another user (taken from a log and translated from German):

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()

Inner exception:

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) 

Any ideas what's wrong with the control, or what I can do to find the source of these exceptions? As I said, these problem occur only every few dozen times the control is instantiated.

4条回答
时光不老,我们不散
2楼-- · 2019-02-21 00:06

I think that we are seeing the same problem. I just get the invalidcast exception more frequently. When, through blind commenting of stuff, I can suppress the invalidcast, I do sometimes get the attribute missing error for an attribute that is, in fact, there. Have you found a solution?

BTW: the easy way to repro it is to just load the offending app and repeatedly hit F5 to refresh the page. You will get it eventually.

I have also seen an intermittent InvalidOperationException that is through from DependencyObject.get_NativeObject().

see my SO question here: Intermittent InavlidCastException in a UserControl.InitializeComponent()

See my silverlight.net bug report here: http://silverlight.net/forums/t/67732.aspx

查看更多
SAY GOODBYE
3楼-- · 2019-02-21 00:13

I've also came across this issue. I do not have a good explanation for that (seems to be XAML parse bug), but I've fixed it by adding following code to XAML:

<UserControl.Resources>
   <customNamespace:InheritedControl x:Name="dummyInstance"/>
</UserControl.Resources>
查看更多
Viruses.
4楼-- · 2019-02-21 00:24

I'm no XAML expert but are you missing a namespace on Points (e.g. l:Points)?

查看更多
家丑人穷心不美
5楼-- · 2019-02-21 00:26

I had the same problem. In my case error was on one of members. Exception was thrown on members constructor.
I solved it by putting breakpoint before InitializeComponent() method and on debug mode pressed F11 to Step Into and found error.
Hope this will help.
Thanks.

查看更多
登录 后发表回答