wpf 如何在包含双引号和单引号的 XAML 中显示文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1103337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:41:53  来源:igfitidea点击:

How to display a text in XAML which contains double and single quotation marks?

wpfxaml

提问by Edward Tanguay

In my XAML file I want to display this text which contains doubleandsinglequotation marks:

在我的 XAML 文件中,我想显示包含引号引号的文本:

You shouldn't choose "Copy if New".

您不应该选择“如果新建则复制”。

None of these work:

这些都不起作用:

<TextBlock Text="You shouldn't choose "Copy if New":"/>
<TextBlock Text="You shouldn't choose ""Copy if New"":"/>
<TextBlock Text="You shouldn't choose \"Copy if New\":"/>
<TextBlock Text='You shouldn't choose \"Copy if New\":'/>
<TextBlock Text='You shouldn\'t choose \"Copy if New\":'/>

I give up, can I do this in XAML?

我放弃了,我可以在 XAML 中做到这一点吗?

回答by Julien Poulin

You should encode the special characters:

您应该对特殊字符进行编码:

<TextBlock Text='You shouldn&apos;t choose &quot;Copy if New&quot;:'/>
  • &apos;for '
  • &quot;for "
  • &apos;为了 '
  • &quot;为了 "

回答by Steve Gilham

There are defined XML escapes &amp;&quot;for " and &amp;&apos;for ' -- if the XML handling in XAMLdoesn't interpret those properly, then start to worry.

&amp;&quot;为 " 和&amp;&apos;'定义了 XML 转义——如果 XML 处理XAML没有正确解释这些,那么开始担心。

回答by Iván Gimeno

If you don't want to encode special chars you could use:

如果您不想编码特殊字符,您可以使用:

<TextBlock>
  <TextBlock.Text>
     You shouldn't choose "Copy if New":
  <TextBlock.Text>
</TexBlock>