2008-10-23 76 views
0

有時得到我的自定義控件以下異常:XamlParseException:自定義控件中的屬性缺失,但已定義!

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) 

任何想法是什麼控制權是否錯誤,或者我能做些什麼來找到這些例外的來源?正如我所說的,這些問題只發生在控制實例化的每幾十次。

+1

由於這是一個受歡迎的問題,我想跟進:在這種情況下,我們從未發現問題。自從我開始工作已經有一段時間了,但我認爲一個團隊成員通過在循環中調用XAML根節點的對象的構造函數來「解決」它,直到沒有拋出異常爲止。 – 2010-09-19 11:22:34

回答

0

我不是XAML專家,但是你缺少點上的命名空間(例如l:Points)嗎?

0

我認爲我們看到了同樣的問題。我只是更頻繁地得到invalidcast異常。當通過盲目評論的東西,我可以抑制無效播放,我有時會得到屬性缺失的錯誤,實際上,這是一個屬性。你找到了解決方案嗎?

順便說一句:簡單的方法來重新載入它只是加載有問題的應用程序,並重復點擊F5刷新頁面。你會最終得到它。

我也看到了從DependencyObject.get_NativeObject()通過的間歇性InvalidOperationException。

看到我的SO問題在這裏: Intermittent InavlidCastException in a UserControl.InitializeComponent()

見我silverlight.net錯誤報告在這裏: http://silverlight.net/forums/t/67732.aspx

1

我也遇到了這個問題。我沒有爲一個很好的解釋(似乎是XAML解析錯誤),但我已經通過添加以下代碼來XAML固定它:

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

我有同樣的問題。在我的情況下,錯誤發生在其中一個成員上。成員構造函數拋出異常。
我通過在InitializeComponent()方法之前放置斷點並在調試模式下將F11按入Step Into並找到錯誤來解決此問題。
希望這會有所幫助。
謝謝。

+0

肯定有例外,請參閱我對原文的評論。但是,這幾乎是隨機的,正如你可以通過我們的「解決方案」所告訴的那樣。謝謝你的信息。 – 2010-12-02 11:47:29