如何使用 UWP MarkupExtension 类? [英] How does one use the UWP MarkupExtension class?

查看:22
本文介绍了如何使用 UWP MarkupExtension 类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

秋季创作者更新 SDK 添加了一个标记扩展类,很棒.https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.markup.markupextension

所以我创建了一个并覆盖了ProvideValue"方法.

公共类 MDL2 : MarkupExtension{...公共字符串目标{获取;放;}受保护的覆盖对象 ProvideValue(){...}}

我试图以这样的方式使用它:

现在,这将正确调用我的 MDL2 扩展的构造函数,并将 Target 属性设置为字符串值Delete".到目前为止一切顺利.

除此之外,从不调用 ProvideValue 覆盖,现在在访问 IconGlyph 的 TemplateBinding 时,我得到 System.Runtime.InteropServices.COMException: Error HRESULT E_FAIL 已从对 COM 组件的调用返回.在 Windows.UI.Xaml.FrameworkElement.MeasureOverride(Size availableSize) 似乎没有尝试从标记扩展中实际获取值.

实际发生的不是调用 ProvideValue 方法,而是实际上将属性值设置为 MarkupExtension 的实例......这不是我想要的,也不是我期望标记扩展工作的方式.

所以,我知道可能不会有很多答案,但是有没有人玩过这个类并让它在 UWP 中很好地工作?这是预期的吗?我的用法很愚蠢吗?

(我以前从未真正以任何形式使用过 MarkupExtension,所以也许我是......)

解决方案

您需要添加 MarkupExtensionReturnType 类的属性:

[MarkupExtensionReturnType(ReturnType = typeof(string))]公共类 MDL2:MarkupExtension{

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

解决方案

You need to add the MarkupExtensionReturnType Attribute to your class:

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

这篇关于如何使用 UWP MarkupExtension 类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆