XAML中的特殊符号几空白字符处理

本文深入探讨了XAML标记语言中特殊符号的处理方式,包括“and”符、>、<、、'等符号的正确使用方法。同时,介绍了如何在XAML代码中处理多空格问题,确保代码的正确解析和显示。通过示例代码展示了具体应用,并提供了Demo下载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

阅读目录

 

介绍

  XAML标记语言是基于xml的,所以很多xml中的特殊符号在XAML也是需要处理的。

 

详细

(取自msdn)

字符

Entity

注释

&(“and”符)

&

必须既用于特性值,又用于元素的内容。

>(大于号字符)

>

必须用于某个特性值,但是,只要前面没有 <,就可以接受 > 作为元素的内容。

<(小于号字符)

&lt;

必须用于某个特性值,但是,只要后面没有 >,就可以接受 < 作为元素的内容。

"(直双引号)

&quot;

必须用于某个特性值,但可接受直引号 (") 作为元素的内容。 请注意,特性值本身可以用单直引号 (') 或直双引号 (”) 引起来;特性值外壳由首先出现的字符定义,另一个引号随后可以用作值中的文本。

'(直单引号)

&apos;

必须用于某个特性值,但可接受单直引号 (') 作为元素的内容。 请注意,特性值本身可以用单直引号 (') 或直双引号 (”) 引起来;特性值外壳由首先出现的字符定义,另一个引号随后可以用作值中的文本。

(数字字符映射)

&#[integer]; 或者 &#x[hex];

XAML 支持将数字字符映射到处于活动状态的编码。

(不间断空格)

&#160;(采用 UTF-8 编码)

对于流文档元素或者使用文本的元素(如 WPF 的 TextBox),不间断空格不会在标记外部规范化,甚至对于 xml:space="default" 也是如此。

 

处理

按照上面的说明,代码如下:

1 <Window x:Class="WpfApplication6.MainWindow"
2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4         Title="MainWindow" Height="350" Width="525">
5     <Grid>
6         <Button Margin="183,131,203,134" FontSize="20" Foreground="Blue">&lt;&quot;按钮&apos;&amp;&gt;</Button>
7     </Grid>
8 </Window>

效果:

多空格的处理,代码如下:

由于多个空格的时候,不做处理的话,xml会把多个空格变成一个空格。

 1 <Window x:Class="WpfApplication6.MainWindow"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         Title="MainWindow" Height="350" Width="525">
 5     <Grid>
 6         <Button Margin="174,45,212,220" FontSize="20" Foreground="Blue">&lt;&quot;按钮&apos;&amp;&gt;</Button>
 7         <TextBox Margin="143,110,163,161" Height="40" FontSize="20" Foreground="Red" xml:space="preserve">"     文本     "</TextBox>
 8         <TextBox Margin="143,175,163,96" Height="40" FontSize="20" Foreground="Black">"文本"</TextBox>
 9         <TextBox Margin="143,221,163,50" Height="40" FontSize="20" Foreground="Blue">" 文本 "</TextBox>
10         <TextBox Margin="143,271,163,0" Height="40" FontSize="20" Foreground="Green">"      文本      "</TextBox>
11     </Grid>
12 </Window>

效果:

 

Demo下载

源码下载

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <!-- ============ 颜色基准 ============ --> <!-- ★ 新增说明详情:暗夜模式颜色与亮色保持语义一致,只调色值 --> <Color x:Key="WindowBgColor">#FF1E1E1E</Color> <Color x:Key="AltBgColor">#FF262626</Color> <Color x:Key="CardBorderColor">#FF383838</Color> <Color x:Key="AccentColor">#FF64B5F6</Color> <Color x:Key="AccentHoverColor">#FF8ABCF7</Color> <Color x:Key="TextFgColor">#FFF2F2F2</Color> <Color x:Key="SubTextFgColor">#FFB0B0B0</Color> <Color x:Key="DisabledFgColor">#FF707070</Color> <Color x:Key="DisabledBgColor">#FF3A3A3A</Color> <Color x:Key="HoverBgColor">#332196F3</Color> <!-- 半透明 Hover --> <Color x:Key="SelectedBgColor">#552196F3</Color> <!-- 半透明选中 --> <Color x:Key="SelectedFgColor">#FFFFFFFF</Color> <Color x:Key="ScrollBarThumbColor">#FF5E5E5E</Color> <!-- ============ 刷子封装 ============ --> <SolidColorBrush x:Key="WindowBgBrush" Color="{StaticResource WindowBgColor}" /> <SolidColorBrush x:Key="AltBgBrush" Color="{StaticResource AltBgColor}" /> <SolidColorBrush x:Key="CardBorderBrush"Color="{StaticResource CardBorderColor}" /> <SolidColorBrush x:Key="AccentBrush" Color="{StaticResource AccentColor}" /> <SolidColorBrush x:Key="AccentHoverBrush" Color="{StaticResource AccentHoverColor}" /> <SolidColorBrush x:Key="TextFgBrush" Color="{StaticResource TextFgColor}" /> <SolidColorBrush x:Key="SubTextFgBrush" Color="{StaticResource SubTextFgColor}" /> <SolidColorBrush x:Key="DisabledFgBrush"Color="{StaticResource DisabledFgColor}" /> <SolidColorBrush x:Key="DisabledBgBrush"Color="{StaticResource DisabledBgColor}" /> <SolidColorBrush x:Key="HoverBgBrush" Color="{StaticResource HoverBgColor}" /> <SolidColorBrush x:Key="SelectedBgBrush"Color="{StaticResource SelectedBgColor}" /> <SolidColorBrush x:Key="SelectedFgBrush"Color="{StaticResource SelectedFgColor}" /> <SolidColorBrush x:Key="ScrollBarThumbBrush" Color="{StaticResource ScrollBarThumbColor}" /> <!-- ============ 全局字体大小保持一致 ============ --> <sys:Double x:Key="FontSizeBase" xmlns:sys="clr-namespace:System;assembly=mscorlib">13</sys:Double> <!-- ============ Window ========= --> <Style TargetType="Window"> <Setter Property="Background" Value="{DynamicResource WindowBgBrush}"/> <Setter Property="Foreground" Value="{DynamicResource TextFgBrush}"/> <Setter Property="FontSize" Value="{DynamicResource FontSizeBase}"/> </Style> <!-- ============ Panel / Border / DockPanel / Grid 默认背景 ============ --> <Style TargetType="{x:Type Panel}"> <Setter Property="Background" Value="{DynamicResource WindowBgBrush}"/> </Style> <Style TargetType="Border"> <Setter Property="Background" Value="{DynamicResource AltBgBrush}"/> <Setter Property="BorderBrush" Value="{DynamicResource CardBorderBrush}"/> <Setter Property="BorderThickness" Value="1"/> </Style> <!-- ============ TextBlock ============ --> <Style TargetType="TextBlock"> <Setter Property="Foreground" Value="{DynamicResource TextFgBrush}"/> </Style> <!-- ============ Button ============ --> <Style TargetType="Button"> <Setter Property="Background" Value="{DynamicResource AccentBrush}"/> <Setter Property="Foreground" Value="White"/> <Setter Property="BorderBrush" Value="{DynamicResource AccentBrush}"/> <Setter Property="BorderThickness" Value="1"/> <Setter Property="Padding" Value="6,2"/> <Setter Property="Cursor" Value="Hand"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="3"> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True"/> </Border> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" Value="{DynamicResource AccentHoverBrush}"/> <Setter Property="BorderBrush" Value="{DynamicResource AccentHoverBrush}"/> </Trigger> <Trigger Property="IsPressed" Value="True"> <Setter Property="Background" Value="{DynamicResource AccentHoverBrush}"/> </Trigger> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Background" Value="{DynamicResource DisabledBgBrush}"/> <Setter Property="Foreground" Value="{DynamicResource DisabledFgBrush}"/> <Setter Property="BorderBrush" Value="{DynamicResource DisabledBgBrush}"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> <!-- ============ ToggleButton / CheckBox / RadioButton 统一色调 ============ --> <Style TargetType="ToggleButton" BasedOn="{StaticResource {x:Type Button}}"/> <Style TargetType="CheckBox"> <Setter Property="Foreground" Value="{DynamicResource TextFgBrush}"/> </Style> <Style TargetType="RadioButton" BasedOn="{StaticResource {x:Type CheckBox}}"/> <!-- ============ TextBox / ComboBox / Slider ============ --> <Style TargetType="TextBox"> <Setter Property="Foreground" Value="{DynamicResource TextFgBrush}"/> <Setter Property="Background" Value="{DynamicResource AltBgBrush}"/> <Setter Property="BorderBrush" Value="{DynamicResource CardBorderBrush}"/> </Style> <Style TargetType="ComboBox" BasedOn="{StaticResource {x:Type TextBox}}"/> <Style TargetType="Slider"> <Setter Property="Foreground" Value="{DynamicResource AccentBrush}"/> </Style> <!-- ============ ProgressBar ============ --> <Style TargetType="ProgressBar"> <Setter Property="Foreground" Value="{DynamicResource AccentBrush}"/> </Style> <!-- ============ ListBox / TreeView 行高亮 ============ --> <Style TargetType="ListBoxItem"> <Setter Property="Foreground" Value="{DynamicResource TextFgBrush}"/> <Setter Property="Background" Value="Transparent"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ListBoxItem"> <Border x:Name="Bd" Background="{TemplateBinding Background}"> <ContentPresenter/> </Border> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="Bd" Property="Background" Value="{DynamicResource HoverBgBrush}"/> </Trigger> <Trigger Property="IsSelected" Value="True"> <Setter TargetName="Bd" Property="Background" Value="{DynamicResource SelectedBgBrush}"/> <Setter Property="Foreground" Value="{DynamicResource SelectedFgBrush}"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style TargetType="TreeViewItem"> <Setter Property="Foreground" Value="{DynamicResource TextFgBrush}"/> <Setter Property="Background" Value="Transparent"/> </Style> <!-- ============ ScrollBar ============ --> <Style TargetType="ScrollBar"> <Setter Property="Background" Value="{DynamicResource AltBgBrush}"/> <Setter Property="Width" Value="12"/> </Style> <Style TargetType="Thumb"> <Setter Property="Background" Value="{DynamicResource ScrollBarThumbBrush}"/> </Style> <!-- ============ ContextMenu / MenuItem ============ --> <Style TargetType="ContextMenu"> <Setter Property="Background" Value="{DynamicResource AltBgBrush}"/> <Setter Property="Foreground" Value="{DynamicResource TextFgBrush}"/> </Style> <Style TargetType="MenuItem"> <Setter Property="Foreground" Value="{DynamicResource TextFgBrush}"/> <Style.Triggers> <Trigger Property="IsHighlighted" Value="True"> <Setter Property="Background" Value="{DynamicResource HoverBgBrush}"/> </Trigger> </Style.Triggers> </Style> </ResourceDictionary><ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <!-- ============ 颜色基准 ============ --> <!-- ★ 新增说明详情:基础颜色,便于后续统一调整 --> <Color x:Key="WindowBgColor">#FFF7F7F7</Color> <!-- 整体背景 --> <Color x:Key="AltBgColor">#FFFFFFFF</Color> <!-- 卡片 / ToolBar 等 --> <Color x:Key="CardBorderColor">#FFE0E0E0</Color> <!-- 卡片边框 --> <Color x:Key="AccentColor">#FF2196F3</Color> <!-- 主高亮色 --> <Color x:Key="AccentHoverColor">#FF42A5F5</Color> <!-- 鼠标悬停高亮 --> <Color x:Key="TextFgColor">#FF212121</Color> <!-- 正文字体 --> <Color x:Key="SubTextFgColor">#FF555555</Color> <!-- 辅助说明文字 --> <Color x:Key="DisabledFgColor">#FF9E9E9E</Color> <!-- 失效文字 --> <Color x:Key="DisabledBgColor">#FFE0E0E0</Color> <!-- 失效背景 --> <Color x:Key="HoverBgColor">#FFE8F1FF</Color> <!-- Hover 行背景 --> <Color x:Key="SelectedBgColor">#FFCCE5FF</Color> <!-- 选中行背景 --> <Color x:Key="SelectedFgColor">#FF000000</Color> <!-- 选中文本 --> <Color x:Key="ScrollBarThumbColor">#FFC0C0C0</Color> <!-- 滚动条滑块 --> <!-- ============ 刷子封装 ============ --> <!-- ★ 新增说明详情:全部用 SolidColorBrush 并标记 Freeze --> <SolidColorBrush x:Key="WindowBgBrush" Color="{StaticResource WindowBgColor}" /> <SolidColorBrush x:Key="AltBgBrush" Color="{StaticResource AltBgColor}" /> <SolidColorBrush x:Key="CardBorderBrush"Color="{StaticResource CardBorderColor}" /> <SolidColorBrush x:Key="AccentBrush" Color="{StaticResource AccentColor}" /> <SolidColorBrush x:Key="AccentHoverBrush" Color="{StaticResource AccentHoverColor}" /> <SolidColorBrush x:Key="TextFgBrush" Color="{StaticResource TextFgColor}" /> <SolidColorBrush x:Key="SubTextFgBrush" Color="{StaticResource SubTextFgColor}" /> <SolidColorBrush x:Key="DisabledFgBrush"Color="{StaticResource DisabledFgColor}" /> <SolidColorBrush x:Key="DisabledBgBrush"Color="{StaticResource DisabledBgColor}" /> <SolidColorBrush x:Key="HoverBgBrush" Color="{StaticResource HoverBgColor}" /> <SolidColorBrush x:Key="SelectedBgBrush"Color="{StaticResource SelectedBgColor}" /> <SolidColorBrush x:Key="SelectedFgBrush"Color="{StaticResource SelectedFgColor}" /> <SolidColorBrush x:Key="ScrollBarThumbBrush" Color="{StaticResource ScrollBarThumbColor}" /> <!-- ============ 全局字体大小 ============ --> <sys:Double x:Key="FontSizeBase" xmlns:sys="clr-namespace:System;assembly=mscorlib">13</sys:Double> <!-- ============ Window ========= --> <!-- ★ 新增说明详情:所有窗口默认背景/前景 --> <Style TargetType="Window"> <Setter Property="Background" Value="{DynamicResource WindowBgBrush}"/> <Setter Property="Foreground" Value="{DynamicResource TextFgBrush}"/> <Setter Property="FontSize" Value="{DynamicResource FontSizeBase}"/> </Style> <!-- ============ Panel / Border / DockPanel / Grid 默认背景 ============ --> <Style TargetType="{x:Type Panel}"> <Setter Property="Background" Value="{DynamicResource WindowBgBrush}"/> </Style> <Style TargetType="Border"> <Setter Property="Background" Value="{DynamicResource AltBgBrush}"/> <Setter Property="BorderBrush" Value="{DynamicResource CardBorderBrush}"/> <Setter Property="BorderThickness" Value="1"/> </Style> <!-- ============ TextBlock ============ --> <Style TargetType="TextBlock"> <Setter Property="Foreground" Value="{DynamicResource TextFgBrush}"/> </Style> <!-- ============ Button ============ --> <Style TargetType="Button"> <Setter Property="Background" Value="{DynamicResource AccentBrush}"/> <Setter Property="Foreground" Value="White"/> <Setter Property="BorderBrush" Value="{DynamicResource AccentBrush}"/> <Setter Property="BorderThickness" Value="1"/> <Setter Property="Padding" Value="6,2"/> <Setter Property="Cursor" Value="Hand"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="3"> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True"/> </Border> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" Value="{DynamicResource AccentHoverBrush}"/> <Setter Property="BorderBrush" Value="{DynamicResource AccentHoverBrush}"/> </Trigger> <Trigger Property="IsPressed" Value="True"> <Setter Property="Background" Value="{DynamicResource AccentHoverBrush}"/> </Trigger> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Background" Value="{DynamicResource DisabledBgBrush}"/> <Setter Property="Foreground" Value="{DynamicResource DisabledFgBrush}"/> <Setter Property="BorderBrush" Value="{DynamicResource DisabledBgBrush}"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> <!-- ============ ToggleButton / CheckBox / RadioButton 统一色调 ============ --> <Style TargetType="ToggleButton" BasedOn="{StaticResource {x:Type Button}}"/> <Style TargetType="CheckBox"> <Setter Property="Foreground" Value="{DynamicResource TextFgBrush}"/> </Style> <Style TargetType="RadioButton" BasedOn="{StaticResource {x:Type CheckBox}}"/> <!-- ============ TextBox / ComboBox / Slider ============ --> <Style TargetType="TextBox"> <Setter Property="Foreground" Value="{DynamicResource TextFgBrush}"/> <Setter Property="Background" Value="{DynamicResource AltBgBrush}"/> <Setter Property="BorderBrush" Value="{DynamicResource CardBorderBrush}"/> </Style> <Style TargetType="ComboBox" BasedOn="{StaticResource {x:Type TextBox}}"/> <Style TargetType="Slider"> <Setter Property="Foreground" Value="{DynamicResource AccentBrush}"/> </Style> <!-- ============ ProgressBar ============ --> <Style TargetType="ProgressBar"> <Setter Property="Foreground" Value="{DynamicResource AccentBrush}"/> </Style> <!-- ============ ListBox / TreeView 行高亮 ============ --> <Style TargetType="ListBoxItem"> <Setter Property="Foreground" Value="{DynamicResource TextFgBrush}"/> <Setter Property="Background" Value="Transparent"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ListBoxItem"> <Border x:Name="Bd" Background="{TemplateBinding Background}"> <ContentPresenter/> </Border> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="Bd" Property="Background" Value="{DynamicResource HoverBgBrush}"/> </Trigger> <Trigger Property="IsSelected" Value="True"> <Setter TargetName="Bd" Property="Background" Value="{DynamicResource SelectedBgBrush}"/> <Setter Property="Foreground" Value="{DynamicResource SelectedFgBrush}"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> <!-- TreeViewItem 同理 --> <Style TargetType="TreeViewItem"> <Setter Property="Foreground" Value="{DynamicResource TextFgBrush}"/> <Setter Property="Background" Value="Transparent"/> </Style> <!-- ============ ScrollBar ============ --> <Style TargetType="ScrollBar"> <Setter Property="Background" Value="{DynamicResource AltBgBrush}"/> <Setter Property="Width" Value="12"/> </Style> <Style TargetType="Thumb"> <Setter Property="Background" Value="{DynamicResource ScrollBarThumbBrush}"/> </Style> <!-- ============ ContextMenu / MenuItem ============ --> <Style TargetType="ContextMenu"> <Setter Property="Background" Value="{DynamicResource AltBgBrush}"/> <Setter Property="Foreground" Value="{DynamicResource TextFgBrush}"/> </Style> <Style TargetType="MenuItem"> <Setter Property="Foreground" Value="{DynamicResource TextFgBrush}"/> <Style.Triggers> <Trigger Property="IsHighlighted" Value="True"> <Setter Property="Background" Value="{DynamicResource HoverBgBrush}"/> </Trigger> </Style.Triggers> </Style> </ResourceDictionary>严重性 代码 说明 项目 文件 行 抑制状态 详细信息 错误 XLS0307 “Color”是导致 XML 无效的意外标记。应为空白。 AssetStudio G:\我的程序开发\MyApp\AssetStudio\AssetStudio\Themes\DarkTheme.xaml 26 错误 XLS0307 “Color”是导致 XML 无效的意外标记。应为空白。 AssetStudio G:\我的程序开发\MyApp\AssetStudio\AssetStudio\Themes\DarkTheme.xaml 31 错误 XLS0307 “Color”是导致 XML 无效的意外标记。应为空白。 AssetStudio G:\我的程序开发\MyApp\AssetStudio\AssetStudio\Themes\DarkTheme.xaml 32 错误 XLS0307 “Color”是导致 XML 无效的意外标记。应为空白。 AssetStudio G:\我的程序开发\MyApp\AssetStudio\AssetStudio\Themes\DarkTheme.xaml 34 错误 XLS0307 “Color”是导致 XML 无效的意外标记。应为空白。 AssetStudio G:\我的程序开发\MyApp\AssetStudio\AssetStudio\Themes\DarkTheme.xaml 35 错误 XLS0307 “Color”是导致 XML 无效的意外标记。应为空白。 AssetStudio G:\我的程序开发\MyApp\AssetStudio\AssetStudio\Themes\LightTheme.xaml 38 错误 XLS0307 “Color”是导致 XML 无效的意外标记。应为空白。 AssetStudio G:\我的程序开发\MyApp\AssetStudio\AssetStudio\Themes\LightTheme.xaml 43 错误 XLS0307 “Color”是导致 XML 无效的意外标记。应为空白。 AssetStudio G:\我的程序开发\MyApp\AssetStudio\AssetStudio\Themes\LightTheme.xaml 44 错误 XLS0307 “Color”是导致 XML 无效的意外标记。应为空白。 AssetStudio G:\我的程序开发\MyApp\AssetStudio\AssetStudio\Themes\LightTheme.xaml 46 错误 XLS0307 “Color”是导致 XML 无效的意外标记。应为空白。 AssetStudio G:\我的程序开发\MyApp\AssetStudio\AssetStudio\Themes\LightTheme.xaml 47 错误(活动) MC3000 ““Color”是一个意外标记。应为空格。 第 26 行,位置 45。.”XML 无效。 AssetStudio G:\我的程序开发\MyApp\AssetStudio\AssetStudio\Themes\DarkTheme.xaml 26 错误(活动) MC3000 ““Color”是一个意外标记。应为空格。 第 38 行,位置 45。.”XML 无效。 AssetStudio G:\我的程序开发\MyApp\AssetStudio\AssetStudio\Themes\LightTheme.xaml 38
最新发布
06-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值