问题描述
限时送ChatGPT账号..我想创建自己的MarkupExtension
(例如Binding
、TemplateBinding
...)
如何像在 WPF 中那样对通用应用执行此操作?
解决方案我很伤心,但没有.UWP 目前不支持自定义标记扩展.
在某些情况下,您可以通过使用绑定和转换器来解决此问题.例如,要使用资源字符串(不使用 x:Uid
),我有一个实际上不需要值的转换器,只需要一个参数(资源 ID).
例如,我可能如下绑定:
(此绑定不需要路径,但无论如何我都绑定到 Language
,这允许即时切换语言.)
在此处详细了解LocalizeConverter
.
但无论如何.如果我一直在做 WPF,这将是一个标记扩展.Binding
本身当然是一个标记扩展,所以只要你能在它的约束范围内工作,它就是一个可能的解决方法.
编辑
我只想强调克莱门斯在他对 OP 的评论中给出的链接:
https://wpdev.uservoice/forums/110705-universal-windows-platform/suggestions/7232264-add-markup-extensions-to-and-improve-winrt-xaml>
更新
现在对自定义的支持有限标记扩展!(迈克尔霍克评论的帽子提示.)
I would like to create my own MarkupExtension
(like Binding
, TemplateBinding
...)
How can I do it for Universal Apps like I did in WPF?
解决方案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/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.)
这篇关于UWP 中的自定义标记扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论