一、问题场景
在首次自定义控件时,vs创建对应的Generic.xaml样式后,为控件样式添加中文内容后,“给定编码中的字符无效。 第 xx 行,位置 xx。.”XML 无效。
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:XXXX"> <Style TargetType="{x:Type local:CustomControl1}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type local:CustomControl1}"> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> <Button Content="测试"></Button> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> </ResourceDictionary>
二、解决方案
将对应报错的xxxxxx.xaml页面编码由ANSI修改为UTF-8即可,个人猜测应该是Generic.xaml默认默认生成编码为ANSI,其他自建XXXX.xaml编码,默认并没有这样的问题。