.XamlReader.Load()解决给定编码中的字符无效与无法创建未知类型

为实现ArcGIS地图上标绘标注信息,如下图红色框选中部份

 

这个边框效果需要引用DLL:Microsoft.Expression.Drawing

与XAML的命名空间:http://schemas.microsoft.com/expression/2010/drawing

加载的XAML标签为:

<ControlTemplate   
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing" >
<ed:Callout AnchorPoint="0.139,1.462" CalloutStyle="Oval" Content="提示" Margin="0" Stroke="#FF2E50BA" Fill="#FFF5F4F4" StrokeThickness="1" Width="100" Height="60"/>

</ControlTemplate>


用如下方法,报错:

      string strXaml = "<ControlTemplate   xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"       xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\"  xmlns:ed=\"http://schemas.microsoft.com/expression/2010/drawing\"  > <ed:Callout AnchorPoint=\"0.139,1.462\" CalloutStyle=\"Oval\" Content=\"提示\" Margin=\"0\"  Stroke=\"#FF2E50BA\"  Fill=\"#FFF5F4F4\" StrokeThickness=\"1\"   Width=\"100\"   Height=\"60\"/> </ControlTemplate>";
_markerSymbol.ControlTemplate = (ControlTemplate)System.Windows.Markup.XamlReader.Parse(strXaml);

无法创建未知类型“{http://schemas.microsoft.com/expression/2010/drawing}Callout”。

原本以为在工作里面引用DLL:Microsoft.Expression.Drawing

和在页面加命名空间:xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing"

还是不能解决问题,继续报错

后来在网上搜索到,这个解决办法,使用ParserContext(类描述为: 提供 XAML 分析器所需的上下文信息。)

通过引用外部的DLL

代码如下:

         ParserContext pc = new ParserContext();
pc.XamlTypeMapper = new XamlTypeMapper(new string[] { });
pc.XamlTypeMapper.AddMappingProcessingInstruction("http://schemas.microsoft.com/expression/2010/drawing", "Microsoft.Expression.Controls", "Microsoft.Expression.Drawing");
pc.XmlnsDictionary.Add("ed", "http://schemas.microsoft.com/expression/2010/drawing");
string strXaml = "<ControlTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" xmlns:ed=\"http://schemas.microsoft.com/expression/2010/drawing\" > <ed:Callout AnchorPoint=\"0.139,1.462\" CalloutStyle=\"Oval\" Content=\"提示\" Margin=\"0\" Stroke=\"#FF2E50BA\" Fill=\"#FFF5F4F4\" StrokeThickness=\"1\" Width=\"100\" Height=\"60\"/> </ControlTemplate>";
_markerSymbol.ControlTemplate = (ControlTemplate)System.Windows.Markup.XamlReader.Parse(strXaml,pc);

这个办法在加载的XAML标签中没有中文的情况下,没有问题,

有中文的话,又报错啦......

给定编码中的字符无效。

纠结了很久找到了正确的解决办法

   ParserContext pc = new ParserContext();
pc.XamlTypeMapper = new XamlTypeMapper(new string[] { });
pc.XamlTypeMapper.AddMappingProcessingInstruction("http://schemas.microsoft.com/expression/2010/drawing", "Microsoft.Expression.Controls", "Microsoft.Expression.Drawing");
pc.XmlnsDictionary.Add("ed", "http://schemas.microsoft.com/expression/2010/drawing");
string strXaml = "<ControlTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" xmlns:ed=\"http://schemas.microsoft.com/expression/2010/drawing\" > <ed:Callout AnchorPoint=\"0.139,1.462\" CalloutStyle=\"Oval\" Content=\"提示\" Margin=\"0\" Stroke=\"#FF2E50BA\" Fill=\"#FFF5F4F4\" StrokeThickness=\"1\" Width=\"100\" Height=\"60\"/> </ControlTemplate>";
_markerSymbol.ControlTemplate = (ControlTemplate)System.Windows.Markup.XamlReader.Load(new MemoryStream(Encoding.UTF8.GetBytes(strXaml)), pc);

关键是这句:

new MemoryStream(Encoding.UTF8.GetBytes(strXaml))将编码转换成UTF-8的流文件



 



posted @   liangwei389  Views(3124)  Comments(2Edit  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
点击右上角即可分享
微信分享提示