【问题标题】:using tab and carriage return character in a WPF resource dictionary在 WPF 资源字典中使用制表符和回车符
【发布时间】:2011-10-20 11:29:38
【问题描述】:

如何在 WPF XAML 资源字典中使用制表符和回车符?

这对我不起作用:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:system="clr-namespace:System;assembly=mscorlib"
                >

<system:String x:Key="test_Key">Tab doesnt work\tTest\rTest</system:String>
</ResourceDictionary>

当我通过 FindResource("test_key") 检索它时,制表符和回车符都被删除了。

【问题讨论】:

    标签: wpf xaml resourcedictionary


    【解决方案1】:

    如果您想避免这种情况,XAML 解析器会使用空白规范化(根据 MSDN),将 xml:space="preserve" 添加到您的 XML 中:

    <system:String x:Key="test_Key" xml:space="preserve">Tab doesnt work&#x09;Test&#x0d;Test</system:String>
    

    【讨论】:

    • \t 等永远不会转换为特殊字符,如果您使用保留选项,您实际上应该编写一个制表符和一个回车符。
    • 糟糕,忘记将字符切换为编码字符。
    【解决方案2】:

    像这样添加换行符&amp;#x0d;&amp;#x0a; 并使用&amp;#x09; 制表符

    但是,除非您按照 J.Kommer 的建议关闭空白规范化,否则这将不起作用

    【讨论】:

    • 您的订单似乎“错误”,即回车后跟换行。 (问题是关于回车而不是换行)
    • @H.B.回车是 \r = 13 = #x0d 和换行是 \r\n 所以我的订单是“正确的”。
    猜你喜欢
    • 2011-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 2015-09-28
    • 1970-01-01
    相关资源
    最近更新 更多