【发布时间】: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