3

这有效:

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

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

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

为什么?

Farm 是调用应用程序。

4

1 回答 1

2

你所拥有的将在 .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);
于 2010-08-10T02:08:25.810 回答