.NET Framework Developer Center > .NET Development Forums > Windows Presentation Foundation (WPF) > Mapping several clr namespaces from different assemblies into to a single xmlns
Ask a questionAsk a question
 

QuestionMapping several clr namespaces from different assemblies into to a single xmlns

  • Friday, March 16, 2007 10:08 PMHaze5150 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi.

    I use XamlReader to instantiate custom object tree for my application. I have the problem to map namespaces that are distributed among several assemblies into one single namespace in XAML. I know that there are different ways and I started from one that will keep XAML code neat.

    1. In the first assembly (A1) I have root class and some children defined. In another assembly (A2) I have several other classes that will be also used in XAML.

    2. A1, A2 have correctly defined [assembly: XmlnsDefinition("http://mytest/ns", "...")] (Note: they all map to a single xmlns)

    3. From console app I try to load XAML with XamlReader from file. Before it I define

    ParserContext parserContext = new ParserContext();

    XamlTypeMapper mapper = new XamlTypeMapper( new string[] { "A1", "A2"});

    ...

    to let XamlReader know where to get types.

    4. Finally I define a default namespace "http://mytest/ns" in XAML. So far, so good.

    When I try to load XAML I get an error saying that type in A2 is not defined in that namespace. Hm.

    Using AddMappingProcessingInstruction of XamlTypeMapper doesn't help because it only allows one mapping to a single namespace (another strange thing).

    The only way that worked to me was to define mapping directly in XAML (what I tried to prevent )

    xmlns:c="clr-namespace:...;assembly=..."

    So does anybody know if there is a way to map several namespaces from several assemblies to a single xmlns?

    Regards, Alexey

     

All Replies

  • Thursday, November 05, 2009 1:19 AMillef Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I suffered from same problem and I could not find a simple solution. Therefore, I had to use multiple XamlTypeMapper for this case.

    However, I think the class "XamlReaderHelper" in System.Windows.Markup namespace can help you.

    This class is internal, so you have to use Reflector. And you can find the HandleMappingProtocol method in this class.

    This method is used by XamlReader for "xmlns Type Mapping". 



        private void HandleMappingProtocol(string mappingUri)
        {
            MappingParser parser = new MappingParser(mappingUri, "clr-namespace:".Length);
            if (!parser.Parse())
            {
                this.ThrowException("ParserMappingUriInvalid", mappingUri);
            }
            if (!this.XamlTypeMapper.PITable.Contains(mappingUri))
            {
                string assembly = parser.Assembly;
                if (assembly != null)
                {
                    int length = assembly.Length;
                }
                ClrNamespaceAssemblyPair pair = new ClrNamespaceAssemblyPair(parser.Namespace, assembly);
                this.XamlTypeMapper.PITable.Add(mappingUri, pair);
            }
            this.WritePI(mappingUri, parser.Namespace, parser.Assembly);
        }



    Thanks.