wpfxamlxamlparseexceptionxamlreader

WPF XamlReader.Parse Dynamic UI xml Namespaces


I've a dynamic WPF UI wich is described in xaml.

The xaml can be seen below. My Problem is that my Custom namespaces aren't resolved automatically,

To get the xaml loaded I have to set each assembly with its namespace to the parser context like this

var context = new ParserContext();
context.XamlTypeMapper = new XamlTypeMapper(new string[] { });      

context.XmlnsDictionary.Add("Custom","http://myCompany.de");
context.XamlTypeMapper.AddMappingProcessingInstruction("http://myCompany.de","FooNamespace","FooAssemblyName");

Isn't there a better way? I need this dynamically. And I don't want to scan the app domain.

How is this done in the normal "System Xaml Reader" ?

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:Custom="http://myCompany.de"
    x:Class="Foo.Container" 
    mc:Ignorable="d" 
    d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.DataContext>
        <Custom:FooViewModel/>
    </UserControl.DataContext>
    <Grid>

    </Grid>
</UserControl>

Solution

  • Clr-Namespace URI's without an assembly mentioned only work as compiled Xaml.
    

    But in case you provide assembly with namespaces like - ";assembly=AssemblyName" suffix to your Uri, it will be parse successfully.

    But in your case xmlns:Custom="http://myCompany.de", you can't add assembly reference. so, i think the only way out is to add the mapping manually for this case.

    And for custom namespaces belonging to your project, you can use the assembly suffix to get them parse.