【问题标题】:Attached Properties throwing ParseException附加属性抛出 ParseException
【发布时间】:2012-08-23 20:48:39
【问题描述】:

我已按照Adding custom attributes to an element in XAML? 的说明进行操作,但不幸的是,设计师告诉我他找不到该元素,并且在启动程序时我得到一个 XamlParserException 并显示消息 Cannot set unknown member '{clr-命名空间:myNs}MediaElementProperties.MediaId'.

我的设置:

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

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

您知道为什么我不断收到异常吗?

【问题讨论】:

  • 你试过关闭所有设计器,清理解决方案并重建它吗?
  • 你试过DependencyProperty.RegisterAttached而不是DependencyProperty.Register吗?
  • 都试过了。没修。设计师仍然说“在 MediaElementProperties 类型中找不到可附加的属性 MediaId”
  • @AlexanderPacha: 哦,我怎么没看到这个,你绝对必须使用RegisterAttached,如果你仍然收到由某事引起的错误否则。 (例如,尝试再次将程序集添加到 xmlns)

标签: c# wpf xaml attached-properties


【解决方案1】:

附加属性需要通过Zabavsky注册RegisterAttachednoted

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

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

【讨论】:

  • 它在同一个应用程序中。我试过了,但没有解决。
  • 谢谢,这对我有帮助!
猜你喜欢
  • 2020-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-09
  • 1970-01-01
  • 2018-06-27
  • 2011-11-01
相关资源
最近更新 更多