Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does XamlReader throw when I use a ParserContext?

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.

like image 356
CannibalSmith Avatar asked Feb 25 '26 22:02

CannibalSmith


1 Answers

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);
like image 100
Quartermeister Avatar answered Feb 27 '26 14:02

Quartermeister