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

发布于 2024-09-13 18:54:11 字数 402 浏览 0 评论 0原文

这有效:

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 是调用应用程序。

This works:

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

This throws The tag 'Pig' does not exist in XML namespace 'clr-namespace:Farm;assembly=Farm':

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

Why?

Farm is the calling application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

软甜啾 2024-09-20 18:54:11

您所拥有的可以在 .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);

What you have will work in .NET 4.0, but unfortunately not in .NET 3.5. Try using XamlTypeMapper instead:

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

If you wanted to use a namespace prefix, you could declare a clr namespace to xml namespace mapping with the XamlTypeMapper and then declare a namespace prefix for the xml namespace.

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文