Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attached Properties throwing ParseException

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?

like image 616
Alexander Pacha Avatar asked Mar 26 '26 06:03

Alexander Pacha


1 Answers

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"
like image 193
H.B. Avatar answered Mar 28 '26 18:03

H.B.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!