XamlParseException:属性自定义控制缺失,但它的定义!它的、自定义、缺失、属性

2023-09-03 04:40:23 作者:婊里如一

我的有时的得到下面的异常为我的自定义控件:

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

发生XamlParseException 未知的属性点的元素SectionClickableArea [行:10位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()}

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

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

这是对的code部分 SectionClickableArea

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); }
	}

我使用该控件像一个多边形按钮。所以我是从按钮继承。我有类似的问题( E_AG_BAD_PROPERTY_VALUE 上的另一的DependencyProperty string类型,根据行和列中给出,等)本控制星期,但我完全不知道为什么。

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

内部异常:

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.

推荐答案

我也遇到了这个问题。我没有一个很好的解释了(好像是XAML解析错误),但我已经加入以下code到XAML固定它:

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>