我有以下代码的问题:
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
var content = reader.ReadToEnd();
ParserContext context = new ParserContext()
{
BaseUri = new Uri(Configuration.SkinsFolder)
//,XmlLang = "utf-8" // I have tried with this parameter and without it
};
var result = XamlReader.Parse(content, context);
return result;
}
Run Code Online (Sandbox Code Playgroud)
相应的xaml,出现问题:
...
<TextBlock>??????? ???????</TextBlock>
<TextBlock Text="?? ????" />
...
Run Code Online (Sandbox Code Playgroud)
在解析这个xaml期间,我得到了异常:
Invalid character in the given encoding. Line 76, position 167.
at System.Windows.Markup.XamlReaderHelper.RethrowAsParseException(String keyString, Int32 lineNumber, Int32 linePosition, Exception innerException)
at System.Windows.Markup.XamlReaderHelper.Read(XamlNode& xamlNode)
at System.Windows.Markup.XamlParser.ReadXaml(Boolean singleRecordMode)
at System.Windows.Markup.XamlParser._Parse()
at System.Windows.Markup.XamlParser.Parse()
Run Code Online (Sandbox Code Playgroud)
Xaml文件保存为utf-8
谁知道如何加载这个xaml没有这样的问题?提前致谢!
PS:好的,我找到了问题的根源.
加载xaml的正确方法是使用XamlReader.Load方法而不是XamlReader.Parse.在我的情况下,它似乎:
using (Stream stream = new FileStream(source, FileMode.Open))
{
ParserContext context = new ParserContext()
{
BaseUri = new Uri(Configuration.SkinsFolder)
};
var result = XamlReader.Load(stream, context);
return result;
}
Run Code Online (Sandbox Code Playgroud)
谢谢大家!
小智 4
我对德语元音变音字符也遇到了同样的问题。我认为 .NET Framework 中存在错误。尝试使用此函数而不是 XamlReader.Parse(content, context):
public static object Parse(string xamlText, ParserContext parserContext)
{
return System.Windows.Markup.XamlReader.Load((Stream) new MemoryStream(Encoding.UTF8.GetBytes(xamlText)), parserContext);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2403 次 |
| 最近记录: |