.XamlReader.Load()解决给定编码中的字符无效与无法创建未知类型
2011-11-30 16:05
501 查看
为实现ArcGIS地图上标绘标注信息,如下图红色框选中部份
这个边框效果需要引用DLL:Microsoft.Expression.Drawing
与XAML的命名空间:http://schemas.microsoft.com/expression/2010/drawing
加载的XAML标签为:
用如下方法,报错:
无法创建未知类型“{http://schemas.microsoft.com/expression/2010/drawing}Callout”。
原本以为在工作里面引用DLL:Microsoft.Expression.Drawing
和在页面加命名空间:xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing"
还是不能解决问题,继续报错
后来在网上搜索到,这个解决办法,使用ParserContext(类描述为: 提供 XAML 分析器所需的上下文信息。)
通过引用外部的DLL
代码如下:
这个办法在加载的XAML标签中没有中文的情况下,没有问题,
有中文的话,又报错啦......
给定编码中的字符无效。
纠结了很久找到了正确的解决办法
关键是这句:
这个边框效果需要引用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的流文件
相关文章推荐
- 解决 vs 出现Error MC3000 给定编码中的字符无效
- 操作XML 报错:根级别上的数据无效 和 给定编码中的字符无效 解决办法
- XmlDocument.Load XML文件时出现“给定的编码字符无效”异常
- “无法显示 XML 页 - 名称以无效字符开头。”&&“ XML解析错误:未组织好”的解决方法
- XamlReader.Load Silverlight 程序动态创建添加UI元素
- 无法显示XML页名称以无效字符开头处理资源'http://xx'时出错的解决方法
- System.Xml.XmlException: 给定编码中的字符无效。 第 XX 行,位置 YY。
- Android Studio Gradle 导出JavaDocJar提示编码GBK的字符无法映射解决
- 无法显示XML页名称以无效字符开头处理资源'http://xx'时出错的解决方法
- 【C#】对异步请求处理程序IHttpAsyncHandler的理解和分享一个易用性封装 【手记】走近科学之为什么明明实现了IEnumerable<T>的类型却不能调用LINQ扩展方法 【手记】手机网页弹出层后屏蔽底层的滑动响应 【手记】ASP.NET提示“未能创建类型”处理 【Web】一个非常简单的移动web消息框 【手记】解决EXCEL跑SQL遇“查询无法运行或数据库表无法打开...”
- asp.net网站错误: 应用程序中的服务器错误 分析器错误信息: 给定编码中有无效的字符。
- 无法显示 XML 页 - 名称以无效字符开头。处理资源 'http://xxxxx' 时出错 的解决方法
- 解决mysqlclient无法转换无效的时间类型
- vs2010 wpf 给定编码中的字符无效
- 无法显示XML页--名称以无效字符开头。iis处理资源时出错的解决办法
- 自定义安装 Python 后,出现方向键无法用,按下方向键后,终端显示的式相应的字符编码,而不是方向键的功能的解决方法
- 『PHP』UTF8编码页面存入GBK数据时使用iconv遇到无法转码的字符时中断内容丢失及解决方法
- IIS无法调试.net程序的报“名称以无效字符开头”一种解决办法
- vs2010 WPF 给定编码中的字符无效
- 解决"System.AccessViolationException”类型的未经处理的异常在 未知模块(IIS Worker Process 已停止工作)导致无法连接远程数据库的问题