3

I've followed the instruction from Adding custom attributes to an element in XAML? but unfortunately the designer tells me that he can't find the element and when starting the program I get an XamlParserException with the message Cannot set unknown member '{clr-namespace:myNs}MediaElementProperties.MediaId'.

My Setup:

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

Do you have any ideas why I keep getting the exception?

Community
  • 1
  • 1
Alexander Pacha
  • 9,187
  • 3
  • 68
  • 108
  • Have you tried closing all designers, cleaning the solution and rebuilding it? – Emond Aug 29 '12 at 09:26
  • Have you tried `DependencyProperty.RegisterAttached` instead of `DependencyProperty.Register`? – Zabavsky Aug 29 '12 at 09:29
  • Tried it all. Didn't fix it. Designer still says that "The attachable Property MediaId was not found in type MediaElementProperties" – Alexander Pacha Aug 29 '12 at 09:33
  • 1
    @AlexanderPacha: o.O, how did i not see this, you *absolutely have to* use `RegisterAttached`, if you still get an error that is caused by *something else*. (e.g. try adding the assembly to the xmlns again) – H.B. Aug 29 '12 at 09:46

1 Answers1

7

Attached properties need to be registered with RegisterAttached as noted by Zabavsky.

When using the XamlReader you may need to be required to fully qualify your xmlns, even though the code is in the same assembly, i.e.

xmlns:myNs="clr-namespace:MyNamespace;assembly=MyApplication"
Community
  • 1
  • 1
H.B.
  • 166,899
  • 29
  • 327
  • 400