How does one use the UWP MarkupExtension class?
Asked Answered
R

1

5

Fall Creators update SDK added a Markup Extension class, great. https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.markup.markupextension

So I create one and override and "ProvideValue" method.

public class MDL2 : MarkupExtension
{
    ...

    public string Target { get; set; }

    protected override object ProvideValue()
    {
        ...
    }
}

I attempt to use it as such in a style:

<Setter Property="IconGlyph" Value="{u:MDL2 Target='Delete'}" />

Now, this will properly call the constructor for my MDL2 extension, and set the Target property to a string value of "Delete". So far so good.

Except, the ProvideValue override is never called, and now when accessing the TemplateBinding for IconGlyph I get System.Runtime.InteropServices.COMException: Error HRESULT E_FAIL has been returned from a call to a COM component. at Windows.UI.Xaml.FrameworkElement.MeasureOverride(Size availableSize) with seemingly no attempt to actually get the value from the markup extensions.

What's actually happening is instead of Invoking the ProvideValue method, it's actually setting the property value too the instance of the MarkupExtension... which is very not what I want and nor how I'd expect markup extensions to work.

So, I know there's probably not going to be many answers to this, but has anyone played with this class yet and got it working nicely in UWP? Is this expected? Am I being daft in my usage?

(I have never actually used MarkupExtension in any form before so perhaps I am...)

Rosalvarosalyn answered 7/11, 2017 at 11:28 Comment(1)
The missing manual might be located here. I don't understand much of it, but applying the attribute is the first thing I'd try.Intensity
F
6

You need to add the MarkupExtensionReturnType Attribute to your class:

[MarkupExtensionReturnType(ReturnType = typeof(string))]
public class MDL2 : MarkupExtension
{
Frances answered 10/2, 2018 at 9:54 Comment(3)
Thanks - this is actually the answer - I was already doing it but it turns out I using using the the entirely wrong Unicode format in this instance. Though I ended up using x:Bind to bind to static values anyway, as I wanted to run them through static converter functions when using x:Bind in DataTemplates... swings and roundabouts! (Attempting to entirely avoid using IValueConverters these days)Rosalvarosalyn
@Michael What's up, thanks for your answer. I am not able to access the bound value from the extension, i.e. <TextBox IsEnabled="{Binding IsBusy, Converter={ex:Negator}}"/>, can I achieve this functionality in UWP somehow? I was hoping to get the binding value from base.ProvideValue but it always returns null.Fulgurous
@Shimmy see this gist: gist.github.com/michael-hawker/6d49e014015a8e122670437bd5c20fc1Frances

© 2022 - 2024 — McMap. All rights reserved.