附加属性抛出ParseException

时间:2012-08-29 09:19:21

标签: c# wpf xaml attached-properties

我已按照Adding custom attributes to an element in XAML?的说明进行操作,但不幸的是设计师告诉我他找不到该元素,并且在启动程序时,我收到一条XamlParserException,消息无法设置未知成员'{ CLR-名称空间:myns名字} MediaElementProperties.MediaId'

我的设置:

  • 使用命令XamlReader.Load(fileStream)动态加载以显示
  • 的Xaml-Page
  • 内容页面本身使用如下代码:

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

你有什么想法让我继续获得例外吗?

1 个答案:

答案 0 :(得分:6)

附加属性需要RegisterAttached noted Zabavsky注册{/ 3}}。

使用XamlReader时,您可能需要完全限定xmlns,即使代码位于同一个程序集中,即

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