C# 读取包含特殊字符的ResourceDictionary
我有一个例子C# 读取包含特殊字符的ResourceDictionary,c#,wpf,C#,Wpf,我有一个例子ResourceDictionary: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=m
ResourceDictionary
:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:System;assembly=mscorlib"
xml:space="preserve">
<!--New line: 
-->
<s:String x:Key="Example.Line">New
Line</s:String>
</ResourceDictionary>
有没有办法避免用XamlReader
替换特殊字符代码
工作黑客 通过使用以下代码转义特殊字符:
//Replaces the special chars.
var text = File.ReadAllText(file, Encoding.UTF8).Replace("&#", "&#");
File.WriteAllText(file, text, Encoding.UTF8);
var dictionary = new ResourceDictionary { Source = new Uri(Path.GetFullPath(file), UriKind.Absolute) };
我能够在不丢失新行值的情况下加载我的
ResourceDictionary
。不过,我认为这不是最好的办法/ 您可以使用ParserContext
参数来加载XamlReader。加载:
XamlReader.Load(xaml, new ParserContext() { XmlSpace = "preserve" });
更多信息:它仍然返回没有特殊字符的字符串。
XamlReader.Load(xaml, new ParserContext() { XmlSpace = "preserve" });