XAML 與空格XAML and whitespace

了解 XAML 使用的空格處理規則。Learn about the whitespace processing rules as used by XAML.

空格處理Whitespace processing

XAML 中的空白字元是空格、換行字元和 tab 鍵,與 XML 一致。這些會分別對應至 Unicode 值0020、000A 和0009。Consistent with XML, whitespace characters in XAML are space, linefeed, and tab. These correspond to the Unicode values 0020, 000A, and 0009 respectively. 根據預設值,當 XAML 處理器處理在 XAML 檔案的元素之間發現的任何內部文字時,會執行下列空格正規化:By default this whitespace normalization occurs when a XAML processor encounters any inner text found between elements in a XAML file:

  • 東亞字元間的換行字元會遭到移除。Linefeed characters between East Asian characters are removed.
  • 所有空白字元 (空格、換行字元、定位字元) 都會轉換成空格。All whitespace characters (space, linefeed, tab) are converted into spaces.
  • 所有連續的空格會被刪除並取代為一個空格。All consecutive spaces are deleted and replaced by one space.
  • 緊接在開始標記之後的空格會遭到刪除。A space immediately following the start tag is deleted.
  • 緊接在結束標記之前的空格會遭到刪除。A space immediately before the end tag is deleted.
  • 「東亞字元」** 會定義為 Unicode 字元範圍集:U+20000 到 U+2FFFD 與 U+30000 到 U+3FFFD。East Asian characters is defined as a set of Unicode character ranges U+20000 to U+2FFFD and U+30000 to U+3FFFD. 這個子集有時也稱為 CJK 表意文字This subset is also sometimes referred to as CJK ideographs. 如需詳細資訊,請參閱http://www.unicode.org。For more information, see http://www.unicode.org.

「預設」會對應到 xml:space 屬性的預設值所表示的狀態。"Default" corresponds to the state denoted by the default value of the xml:space attribute.

內部文字的空格與字串基本類型Whitespace in inner text, and string primitives

上述正規化規則適用於 XAML 元素中的內部文字。The above normalization rules apply to inner text within XAML elements. 在正規化後,XAML 處理器會將任何內部文字轉換成適當類型,如下所示:After normalization, a XAML processor converts any inner text into an appropriate type like this:

  • 如果屬性的類型不是集合,但也並非 Object 類型,XAML 處理器會嘗試使用其類型轉換器轉換成該類型。If the type of the property is not a collection, but is not directly an Object type, the XAML processor tries to convert to that type using its type converter. 如果轉換失敗,將導致 XAML 剖析錯誤。A failed conversion here results in a XAML parse error.
  • 如果屬性的類型是集合,而內部文字是連續的 (中間沒有元素標記),內部文字會剖析為單一 StringIf the type of the property is a collection, and the inner text is contiguous (no intervening element tags), the inner text is parsed as a single String. 如果集合類型無法接受 String,也會導致 XAML 剖析器錯誤。If the collection type cannot accept String, this also results in a XAML parser error.
  • 如果屬性的類型是 Object,內部文字會剖析為單一 StringIf the type of the property is Object, then the inner text is parsed as a single String. 如果中間有標記元素,會導致 XAML 剖析器錯誤,因為 Object 類型意味著單一物件 (String 或其他)。If there are intervening element tags, this results in a XAML parser error, because the Object type implies a single object (String or otherwise).
  • 如果屬性的類型是集合,而且內部文字不是連續的,第一個子字串會轉換成 String 並新增為集合項目,中間的元素會新增為集合項目,而最後的結尾子字串 (若有的話) 會新增至集合做為第三個 String 項目。If the type of the property is a collection, and the inner text is not contiguous, then the first substring is converted into a String and added as a collection item, the intervening element is added as a collection item, and finally the trailing substring (if any) is added to the collection as a third String item.

空格與文字內容模型Whitespace and text content models

保留空格實際上只是為了所有可能的內容模型的子集考量。In practice, preserving whitespace is of concern only for a subset of all possible content models. 該子集的內容模型由可以接受某些格式的單一 String 類型、專用的 String 集合,或是清單、集合或字典中的 String 與其他類型組合構成。That subset is composed of content models that can take a singleton String type in some form, a dedicated String collection, or a mixture of String and other types in lists, collections, or dictionaries.

即使是可接受字串的內容模型,這些內容模型中的預設行為也都是不會將任何保留的空白字元視為必要。Even for content models that can take strings, the default behavior within these content models is that any whitespace that remains is not treated as significant.

保留空格Preserving whitespace

有數種方法可以在來源 XAML 中保留空格,而不受 XAML 處理器空格正規化的影響。Several techniques for preserving whitespace in the source XAML for eventual presentation are not affected by XAML processor whitespace normalization.

xml:space="preserve":在需要保留空格的元素層級指定此屬性。xml:space="preserve": Specify this attribute at the level of the element where whitespace needs to be preserved. 請注意,它會保留所有空格,包括由程式碼編輯器或設計表面新增的空格,這是為了對齊標記元素以便在視覺上呈現直觀的巢狀結構。Note that this preserves all whitespace, including the spaces that might be added by code editors or design surfaces to align markup elements as a visually intuitive nesting. 這些空格的轉譯與包含元素的內容模型相關。Whether those spaces render is again a matter of the content model for the containing element. 我們不建議您在根層級指定 xml:space="preserve",因為大部分的物件模型不認為空格很重要。We don't recommend that you specify xml:space="preserve" at the root level, because the majority of object models don't consider whitespace as significant one way or another. 比較好的做法是,只有當項目會呈現字串內的空白字元或本身是需要空白字元的集合時,才在項目層級特別設定這個屬性。It is a better practice to only set the attribute specifically at the level of elements that render whitespace within strings, or are whitespace significant collections.

實體與不分行空格:XAML 支援在文字物件模型內放置任何 Unicode 實體。Entities and nonbreaking spaces: XAML supports placing any Unicode entity within a text object model. 您可以使用專用實體,像是不分行空格 (UTF-8 編碼方式)。You can use dedicated entities such as nonbreaking space (in UTF-8 encoding). 您也可以使用支援不分行空格字元的 RTF 文字控制項。You can also use rich text controls that support nonbreaking space characters. 如果您使用實體來模擬配置特性 (像是縮排),由於實體的執行階段輸出將依據比一般的配置設施 (像是適當使用面板與邊界) 更多的因素而變更,因此應該謹慎使用。Be cautious if you are using entities to simulate layout characteristics such as indents, because the run-time output of the entities vary based on a greater number of factors than would the general layout facilities, such as proper use of panels and margins.