none
XmlnsDefinition doesn't work in the same assembly

    Question

  • I get a compiler error when I try to use the XmlnsDefinition attribute.

    How to reproduce:

    1) Create a WPF Application: WPFLocalization

    2) Add a new class: Test.cs

    [assembly: XmlnsDefinition("http://myapp", "WPFLocalization")]  
     
    namespace WPFLocalization  
    {  
        public static class Test  
        {  
            public static string Text { get { return "Hello"; } }  
        }  

    3) Edit the Window1.xaml file

    <Window x:Class="WPFLocalization.Window1" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:local="http://myapp" 
        Title="Window1" Height="300" Width="300">  
        <Grid> 
            <TextBlock Text="{x:Static local:Test.Text}"/>  
        </Grid> 
    </Window> 

    4) Now try to build and you should get the mentioned compiler errror:

    Cannot find the type 'Test'. Note that type names are case sensitive.


    5) Change the xmlns:local definition in the Window1.xaml file

    xmlns:local="clr-namespace:WPFLocalization"

    6) The build should work now.


    Why doesn't work my approach with the XmlnsDefinition attribute?

    Friday, March 13, 2009 4:56 PM

Answers

  • Unfortunately, WPF does not support this within the same assembly.  I'm not sure why, but based on the MSDN description, the only thing I can speculate is that they designed it to only search referenced assemblies, and it doesn't search the source assembly.

    This link to the Programming WPF book confirms this, unfortunately:

    http://books.google.com/books?id=558i6t1dKEAC&pg=PA685&lpg=PA685&dq=XmlnsDefinitionattribute+same+assembly&source=bl&ots=gZriLGDSOR&sig=fqi96DvoBlYmPFG5GmzXfUHY_qE&hl=en&ei=fJS6SdGYD4_OMofLnKMI&sa=X&oi=book_result&resnum=3&ct=result.

    If anyone else knows why this is, and if its going to be fixed in future versions of WPF, please speak up.

    Hope this helps,
    Brian

    Brian Schwalm • www.anythinksolutions.com
    • Marked as answer by Tao Liang Thursday, March 19, 2009 5:57 AM
    Friday, March 13, 2009 5:20 PM

All replies

  • Unfortunately, WPF does not support this within the same assembly.  I'm not sure why, but based on the MSDN description, the only thing I can speculate is that they designed it to only search referenced assemblies, and it doesn't search the source assembly.

    This link to the Programming WPF book confirms this, unfortunately:

    http://books.google.com/books?id=558i6t1dKEAC&pg=PA685&lpg=PA685&dq=XmlnsDefinitionattribute+same+assembly&source=bl&ots=gZriLGDSOR&sig=fqi96DvoBlYmPFG5GmzXfUHY_qE&hl=en&ei=fJS6SdGYD4_OMofLnKMI&sa=X&oi=book_result&resnum=3&ct=result.

    If anyone else knows why this is, and if its going to be fixed in future versions of WPF, please speak up.

    Hope this helps,
    Brian

    Brian Schwalm • www.anythinksolutions.com
    • Marked as answer by Tao Liang Thursday, March 19, 2009 5:57 AM
    Friday, March 13, 2009 5:20 PM
  • Hi,

    The root cause that prevents the usage of the XmlnsDefinition in the same namespace is that, the XAML file must be parsed before the assembly is built in order to generate the code implied by the XAML to be included in the assembly.

    The XAML compiler produces code behind files (the files ending in, for example, .g.cs) and these file then become part of the assembly the XAML file is contained in.

    Since the assembly hasn't been built yet, the XAML compiler cannot load it to determine XmlnsDeclaration attributes have been specified.



    Wa'el Mohsen

    Monday, March 26, 2012 10:46 AM
  • Well, but the compiler wouldn't need to load anything else, it could just start evaluating the attributes it's currently building. I still don't get it why this should not be possible. After all, it's just a mapping. There's aliases for everything all around and they work fine. Still looks pretty much like stupid design.

    Wednesday, February 06, 2013 9:20 AM