2012-08-29 62 views
0

我按照從Adding custom attributes to an element in XAML?的指示,但不幸的是設計者告訴我,他無法找到該元素,當啓動程序時,我得到一個XamlParserException與消息無法設置未知成員'{clr-命名空間:myns名字} MediaElementProperties.MediaId」附加屬性拋出ParseException

我的設定:

  • XAML的頁與該命令XamlReader.Load(fileStream)動態加載用於顯示
  • 內容頁本身,它使用這樣的代碼:

    <MediaElement myNs:MediaElementProperties.MediaId="test" ... /> 
    

    其中myns名字用

    xmlns:myNs="clr-namespace:MyNamespace" 
    
  • 定義
  • 而且MediaElementProperties看起來像這樣的定義:

    namespace MyNamespace { 
    public static class MediaElementProperties 
    { 
        public static readonly DependencyProperty MediaIdProperty = 
         DependencyProperty.Register("MediaId", typeof(string), typeof(MediaElementProperties), new FrameworkPropertyMetadata(string.Empty)); 
    
        public static string GetMediaId(UIElement element) 
        { 
         return (string)element.GetValue(MediaIdProperty); 
        } 
    
        public static void SetMediaId(UIElement element, string value) 
        { 
         element.SetValue(MediaIdProperty, value); 
        } 
    }} 
    

你有什麼想法,爲什麼我不斷收到異常?

+0

你嘗試過關閉所有的設計師,清洗解決方案,並重建它? –

+0

你試過'DependencyProperty.RegisterAttached',而不是'DependencyProperty.Register'? – Zabavsky

+0

試了一下。沒有修復它。設計師還在說 –

回答

4

附加屬性需要註冊RegisterAttachednotedZabavsky

當使用XamlReader你可能需要需要完全限定的xmlns,即使代碼是在同一程序,即

xmlns:myNs="clr-namespace:MyNamespace;assembly=MyApplication" 
+0

這是相同的應用程序內。我試過了,但沒有解決。 –

+0

@AlexanderPacha:那麼,也許是隻需要在'XamlReader.Parse' ... –

+0

謝謝,幫我了! – staafl