标记铸件仍在编码字符串

发布于 2025-02-09 07:56:36 字数 471 浏览 1 评论 0原文

我发现了许多如何使用MarkupString的示例,而它只是不做应该的事情。

在我的大火组件中,

            <small class="help-text editable-label" @ref="HelpText">
                @(!string.IsNullOrWhiteSpace(Model.HelpText) ? (MarkupString)Model.HelpText : "Double-click here to add text")
            </small>

我现在有一个简单的模型,在该模型中,我将Helptext初始化到

"<b><i>This is a test</b></i>"

我的网页中,我可以显示出精确的输出。 (请参阅附件)。

I've found numerous examples of how to use MarkupString and it's just not doing what it's supposed to.

In my blazor component I have:

            <small class="help-text editable-label" @ref="HelpText">
                @(!string.IsNullOrWhiteSpace(Model.HelpText) ? (MarkupString)Model.HelpText : "Double-click here to add text")
            </small>

I have a simple model right now where I initialize HelpText to

"<b><i>This is a test</b></i>"

In my webpage I get that exact output displayed. (See attached picture).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌若浮生 2025-02-16 07:56:36

这实际上是对的变化?:的变化。以某种方式将标记标记值转换为对象?,然后再次转换为String

老实说,我感到惊讶的是您的代码编译,的两个分支?:必须是'sigsment兼容',并且不能将标记串分配给字符串。

您可以做到这一点:

<small class="help-text editable-label" @ref="HelpText">
  @((MarkupString)(!string.IsNullOrWhiteSpace(ModelHelpText) ? ModelHelpText : "Double-click here to add text"))
</small>

但是,这变得如此丑陋,我会将其移至@Code部分。

This is actually do to the vagaries of ?:. Somehow the MarkupString value is converted to object? and then to string again.

To be honest I was surprised your code compiles, the two branches of ?: must be 'assignment compatible', and MarkupString cannot be assigned to string.

You can do this:

<small class="help-text editable-label" @ref="HelpText">
  @((MarkupString)(!string.IsNullOrWhiteSpace(ModelHelpText) ? ModelHelpText : "Double-click here to add text"))
</small>

but that is getting so ugly I would move it to the @code section.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文