Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one use the UWP MarkupExtension class?

Fall Creators update SDK added a Markup Extension class, great. https://docs.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...)

like image 539
Johnny Westlake Avatar asked Nov 07 '17 11:11

Johnny Westlake


1 Answers

You need to add the MarkupExtensionReturnType Attribute to your class:

[MarkupExtensionReturnType(ReturnType = typeof(string))]
public class MDL2 : MarkupExtension
{
like image 83
Michael Hawker - MSFT Avatar answered Oct 02 '22 06:10

Michael Hawker - MSFT