Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deal with spaces in ​ XAML

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to deal with spaces in XAML. The article is very detailed and has certain reference value. Interested friends must read it!

How to handle spaces in XAML is described in detail in MSDN: msdn.microsoft.com/en-us/library/cc189036%28v=VS.95%29.aspx#whitespace

To summarize the important points:

Spaces, carriage returns, and tabs are treated as spaces.

Consecutive spaces are merged into one space.

The space immediately following is ignored. Immediately preceding spaces are ignored. In other words, the parser trims the string in the middle of...

How to preserve spaces

attribute xml:space="preserve"

When this property is set, all spaces in the element are preserved, consecutive spaces are not merged, and spaces at both ends are not trimmed.

An inconvenience with this method is that it preserves all spaces in the element, even if they result from formatting. This means that you can't use shortcuts like ctrl+E,F to clean up the code format, and once you accidentally clean it up, it's very troublesome to change it back.

Because XAML supports writing Unicode code directly, this string represents a non breaking space.

complex text

RichTextBox is recommended for text with complex formatting.

Because text elements are divided into block and inline, TextBlock is only allowed to contain inline, so it is more appropriate to display a small amount of text.

If the format is complex, pay attention to using span well. The difference between span and run is that span does not inherit the format of the parent element, which is equivalent to resetting the format and is not affected by the format of the parent element.

* newline

Enter carriage returns in XAML will be treated as a space, all line feeds will be used.

In addition, two runs are adjacent, if they are arranged as follows:

abcd

Then it appears as:

abcd

A newline character is automatically added between two Run characters.

If you want carriage returns removed and displayed as abcd, you should write in one line:

abcd

* Tab

First of all, there are no tabs in XAML; you can only replace them with consecutive spaces.

Note that when assigning a string to the Xaml attribute of RichTextBox, consecutive spaces will be merged, so if you want to keep tabs (consecutive spaces), you need to do such a workaround:

private const string TAB = " ";private const string TAB_PLACEHOLDER = "===TAB===";textBox1.Text = richTextBox1.Xaml;string xaml = richTextBox1.Xaml;xaml = xaml.Replace(TAB, TAB_PLACEHOLDER);richTextBox2.Xaml = xaml;foreach (Block block in richTextBox2.Blocks){ foreach (Inline inline in ((Paragraph)block).Inlines) { ((Run)inline).Text = ((Run)inline).Text.Replace(TAB_PLACEHOLDER, TAB); }}

Replace the four spaces with a placeholder so that the spaces are not merged when assigning values to the richTextBox's xaml attribute.

Then replace this placeholder, and no merge occurs when the value is replaced.

==================================================

XAML is bound by XML rules. For example, XML has special meaning for several special characters, such as &,. If you try to set the content of an element with these values, you may make an error because the XAML parser assumes that you are trying to do something, such as creating nested elements. You can use character entities such as,& with &,"with" special characters not one obstacle for you to run XAML, the other is space handling. Before and after a string, spaces in the string, TAB, and Enter are ignored, leaving only one space. You can use XML:space="preserve", keep any spaces, TAB, Enter. Note that these rules are only useful for XAML, and if you set content in your code, any whitespace is preserved.

Special characters and spaces

The following characters are defined as whitespace characters in [XML]:

Space ( )

Tab ( )

Enter ( )

New line ( )

That's all for "How to handle spaces in XAML." Thanks for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report