我按照添加自定义属性到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); } }}
您有什么想法,为什么我一直收到异常?
DependencyProperty.RegisterAttached
代替DependencyProperty.Register
吗? - ZabavskyRegisterAttached
,如果你仍然遇到错误,那是由其他原因引起的。(例如,尝试再次将程序集添加到xmlns中) - H.B.