How does one use the UWP MarkupExtension class?

橙三吉。 提交于 2019-12-02 00:45:31

问题


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...)


回答1:


You need to add the MarkupExtensionReturnType Attribute to your class:

[MarkupExtensionReturnType(ReturnType = typeof(string))]
public class MDL2 : MarkupExtension
{


来源:https://stackoverflow.com/questions/47156782/how-does-one-use-the-uwp-markupextension-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!