附加属性引发ParseException

Ale*_*cha 1 c# wpf xaml attached-properties

我遵循了向XAML中的元素添加自定义属性中的说明但是不幸的是,设计师告诉我他找不到元素,并且在启动程序时收到XamlParserException消息,提示消息无法设置未知成员'{clr-namespace:myNs} MediaElementProperties.MediaId'

我的设置:

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

    <MediaElement myNs:MediaElementProperties.MediaId="test" ... />
    
    Run Code Online (Sandbox Code Playgroud)

    myNs定义为

     xmlns:myNs="clr-namespace:MyNamespace"
    
    Run Code Online (Sandbox Code Playgroud)
  • 以及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);
        }
    }}
    
    Run Code Online (Sandbox Code Playgroud)

您有什么想法为什么我会不断收到例外?

H.B*_*.B. 6

RegisterAttachedZabavsky 所述,必须向附加属性进行注册。

使用时,即使代码在同一程序集中,也XamlReader可能需要完全验证您的资格xmlns,即

xmlns:myNs="clr-namespace:MyNamespace;assembly=MyApplication"
Run Code Online (Sandbox Code Playgroud)