我有時得到我的自定義控件以下異常:XamlParseException:自定義控件中的屬性缺失,但已定義!
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)
任何想法是什麼控制權是否錯誤,或者我能做些什麼來找到這些例外的來源?正如我所說的,這些問題只發生在控制實例化的每幾十次。
由於這是一個受歡迎的問題,我想跟進:在這種情況下,我們從未發現問題。自從我開始工作已經有一段時間了,但我認爲一個團隊成員通過在循環中調用XAML根節點的對象的構造函數來「解決」它,直到沒有拋出異常爲止。 – 2010-09-19 11:22:34