当我使用ParserContext时,为什么XamlReader会抛出?

时间:2010-08-09 22:40:43

标签: wpf xml xaml xamlreader

这有效:

XamlReader.Parse("<Pig xmlns=\"clr-namespace:Farm;assembly=Farm\"/>");

抛出标签'Pig'在XML命名空间'clr-namespace:Farm; assembly = Farm'中不存在:

var context = new ParserContext();
context.XmlnsDictionary.Add("", "clr-namespace:Farm;assembly=Farm");
XamlReader.Parse("<Pig/>", context);

为什么?

Farm是调用应用程序。

1 个答案:

答案 0 :(得分:1)

你在.NET 4.0中可以使用什么,但遗憾的是在.NET 3.5中没有。请尝试使用XamlTypeMapper:

var context = new ParserContext();
context.XamlTypeMapper = new XamlTypeMapper(new string[] { });
context.XamlTypeMapper.AddMappingProcessingInstruction("", "Farm", "Farm");
XamlReader.Parse("<Pig/>", context);

如果要使用名称空间前缀,可以使用XamlTypeMapper将clr名称空间声明为xml名称空间映射,然后为xml名称空间声明名称空间前缀。

var context = new ParserContext();
context.XamlTypeMapper = new XamlTypeMapper(new string[] { });
context.XamlTypeMapper.AddMappingProcessingInstruction("Foo", "Farm", "Farm");
context.XmlnsDictionary.Add("a", "Foo");
XamlReader.Parse("<a:Pig/>", context);