.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 @ 2011-11-30 16:05  liangwei389  Views(3110)  Comments(2Edit  收藏  举报