所以,我知道可能不会有很多答案,但是有没有人玩过这个类并让它在 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.markupextensionSo 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 类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-26 21:56