Custom MarkupExtension in UWP

无人久伴 提交于 2019-12-10 16:07:42

问题


I would like to create my own MarkupExtension (like Binding, TemplateBinding...)

How can I do it for Universal Apps like I did in WPF?


回答1:


I has a sad, but no. UWP doesn't currently support custom markup extensions.

In some cases you can work around this by using bindings and converters. For example, to use a resource string (not using x:Uid), I have a converter that doesn't actually need a value, only a parameter (the resource id).

For example, I might bind as follows:

<TextBlock
    Text="{x:Bind Language, Mode=OneWay, Converter={StaticResource Localize}, ConverterParameter=MyResourceId}"
/>

(This binding doesn't need a path, but I bind to Language anyway, which allows switching languages on the fly.)

More about the LocalizeConverter here.

But anyway. If I'd been doing WPF, this would have been a markup extension. Binding itself is, of course, a markup extension, so as long as you can work within its constraints, it is a possible workaround.

EDIT

I just want to highlight the link Clemens gave in his comment to the OP:

https://wpdev.uservoice.com/forums/110705-universal-windows-platform/suggestions/7232264-add-markup-extensions-to-and-improve-winrt-xaml

UPDATE

There is now limited support for custom markup extensions! (Hat tip to Michael Hawker's comment.)



来源:https://stackoverflow.com/questions/41245320/custom-markupextension-in-uwp

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