UWP 中的自定义标记扩展

编程入门 行业动态 更新时间:2024-10-25 12:16:30
本文介绍了UWP 中的自定义标记扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我想创建自己的MarkupExtension(例如BindingTemplateBinding...)

如何像在 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:关键词]

本文发布于:2023-04-28 09:25:38,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   标记   UWP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!