附加属性引发ParseException异常

3

我按照添加自定义属性到XAML元素的指示进行了操作,但不幸的是设计师告诉我他无法找到该元素,并且在启动程序时我收到了一条XamlParserException消息,内容为Cannot set unknown member '{clr-namespace:myNs}MediaElementProperties.MediaId'

我的设置:

  • Xaml-Page that is loaded dynamically with the command XamlReader.Load(fileStream) for displaying
  • The content page itself which uses the code like this:

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

    where myNs was defined with

     xmlns:myNs="clr-namespace:MyNamespace"
    
  • And the Definition of the MediaElementProperties which looks like this:

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

您有什么想法,为什么我一直收到异常?


你尝试过关闭所有设计器,清理解决方案并重新构建吗? - Emond
你尝试过使用 DependencyProperty.RegisterAttached 代替 DependencyProperty.Register 吗? - Zabavsky
尝试了所有方法,但问题仍未解决。设计师仍然表示:“在MediaElementProperties类型中找不到可附加的属性MediaId”。 - Alexander Pacha
1
@AlexanderPacha:哦.0,我怎么没看到这个,你绝对必须使用RegisterAttached,如果你仍然遇到错误,那是由其他原因引起的。(例如,尝试再次将程序集添加到xmlns中) - H.B.
1个回答

7

需要使用RegisterAttached方法进行注册的附加属性,由Zabavsky进行注释

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

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

它在同一个应用程序中。我尝试过了,但是没有解决问题。 - Alexander Pacha
谢谢,这对我很有帮助! - staafl

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接