完全按照 XML 文字的方式编写 XML

发布于 2024-08-19 05:29:26 字数 1877 浏览 7 评论 0原文

我正在将一些 XML 从 DrawingML 转换为 XAML。不幸的是,XAML 无法按预期处理空白,但我找到了一种解决方法。问题如下:

问题陈述

我想在 TextBlock 中写入以下内容:

约翰寿司 A 对他说了什么 寿司B?

所以我会写:

<TextBlock>
 <Run>Hey</Run>
 <Run FontWeight="Bold">John</Run>
 <Run>,</Run>
 <Run FontStyle="Italic">what did Sushi A say to Sushi B?</Run>
</TextBlock>

这不会产生预期的结果。相反,它会产生:

约翰寿司 A 对他说了什么 寿司B?

注意到“John”和“,”之间的空格了吗?很奇怪,嗯?这是因为 XAML 在运行之间附加了一个空格。我不知道它为什么这样做。我确实需要与上面完全相同的格式,因此更改格式的选项(例如将逗号设置为粗体)不是一个选项。

部分解决方案

更奇怪的是,有一种方法可以解决这个问题 - 即失去 XAML 添加的额外空间 - 您必须将运行放在同一行上。我不知道为什么,但事实就是如此。因此,以下内容实际上工作得很好:

<TextBlock>
 <Run>Hey</Run>
 <Run FontWeight="Bold">John</Run><Run>,</Run>
 <Run FontStyle="Italic">what did Sushi A say to Sushi B?</Run>
</TextBlock>

注意运行 #2 和 #3(共 4 次运行)现在位于同一行。

问题

我遇到的问题是我还没有找到使用 XML Literals 编写上述内容的方法。如果我尝试这样做:

Dim tb = <TextBlock>
             <Run>Hey</Run>
             <Run FontWeight="Bold">John</Run><Run>,</Run>
             <Run FontStyle="Italic">what did Sushi A say to Sushi B?</Run>
         </TextBlock>

总是创建如下,4个运行在单独的行上:

<TextBlock>
 <Run>Hey</Run>
 <Run FontWeight="Bold">John</Run>
 <Run>,</Run>
 <Run FontStyle="Italic">what did Sushi A say to Sushi B?</Run>
</TextBlock>

有人知道如何完全按照 XML Literals 中的方式编写 XML 吗?< /em>

奖励

如果你正确回答了问题,我会告诉你这个笑话的妙语:)

I'm transforming some XML from DrawingML to XAML. Unfortunately, the XAML one is not working as expected with white spaces, but I have found a work around. Here's the problem:

Problem Statment

I want to write the following in a TextBlock:

Hi John, what did Sushi A say to
Sushi B?

So I would write:

<TextBlock>
 <Run>Hey</Run>
 <Run FontWeight="Bold">John</Run>
 <Run>,</Run>
 <Run FontStyle="Italic">what did Sushi A say to Sushi B?</Run>
</TextBlock>

This doesn't produce the desired results. Instead, it produces:

Hi John , what did Sushi A say to
Sushi B?

Notice the space now between "John" and ","? Weird, eh? This is because XAML appends a space between runs. I don't know why it does this. I really do need the formatting exactly as above, so the option of changing formatting, like making the comma bold too is not an option.

Partial Solution

The weirder thing is that there is a way around this - i.e. to lose the extra space that XAML adds - you have to put your runs on the same line. I have no idea why, but that's the case. So the following actually works just fine:

<TextBlock>
 <Run>Hey</Run>
 <Run FontWeight="Bold">John</Run><Run>,</Run>
 <Run FontStyle="Italic">what did Sushi A say to Sushi B?</Run>
</TextBlock>

Notice runs #2 and #3 (of 4 runs) are now on the same line.

Question

The issue I'm having is that I haven't found a way to write the above using XML Literals. If I try this:

Dim tb = <TextBlock>
             <Run>Hey</Run>
             <Run FontWeight="Bold">John</Run><Run>,</Run>
             <Run FontStyle="Italic">what did Sushi A say to Sushi B?</Run>
         </TextBlock>

it is always created as the below, with the 4 runs on seperate lines:

<TextBlock>
 <Run>Hey</Run>
 <Run FontWeight="Bold">John</Run>
 <Run>,</Run>
 <Run FontStyle="Italic">what did Sushi A say to Sushi B?</Run>
</TextBlock>

Does anyone know how XML can be written exactly as written in XML Literals?

Bonus

If you answer the question correctly, I'll tell you the punchline of the joke :)

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

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

发布评论

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

评论(2

剧终人散尽 2024-08-26 05:29:26

我不认为使用跨度会对您有帮助(因为它会将非格式化文本保留在 XML 元素之外,因此它可能不会自动格式化)。

<TextBlock>
    <Span>                
        Hey
        <Bold>John</Bold>, 
        <Italic>what did Sushi A say to Sushi B?</Italic>
    </Span>            
</TextBlock>

显然这只能修复特定情况而不是一般情况,我可能建议不要使用 XML 文字:)

I don't suppose using a span will help you (as it will keep non-formatted text out of XML elements so it might not get auto-formatted).

i.e.

<TextBlock>
    <Span>                
        Hey
        <Bold>John</Bold>, 
        <Italic>what did Sushi A say to Sushi B?</Italic>
    </Span>            
</TextBlock>

Obviously this only fixes the specific case not the general, I would probably suggest not using XML literals :)

等数载,海棠开 2024-08-26 05:29:26

unicode 退格字符有可能解决您的问题吗?

http://www.fileformat.info/info/unicode/char/ 0008/index.htm

更新

另一种想法。您是否研究过 XDocument.Save(TextWriter textWriter, SaveOptions saveOptions) 方法? 文档说,如果您使用SaveOptions.DisableFormatting< /code>,它将保留间距。

Any chance the unicode backspace character would solve your problem?

http://www.fileformat.info/info/unicode/char/0008/index.htm

Update

One other idea. Have you looked into the XDocument.Save(TextWriter textWriter, SaveOptions saveOptions) method? The documentation says that if you use SaveOptions.DisableFormatting, it will preserve spacing.

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