locked
?? Can I Specify the Global Namespace in clr-namespace ?? RRS feed

  • Question

  • Hello,

    I created a User Control which I would prefer to have live in the global namesapce. For example, here's a minimum User Control and its associated code behind that live in the global namespace:

    <UserControl x:Class="MyUserControl"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    	<DockPanel>
    	</DockPanel>
    </UserControl>
    using System.Windows.Controls;
    
    public partial class MyUserControl : UserControl {
       public FontPreviewControl() {
          InitializeComponent();
       }
    }
    You see? It lives in the global namesapce.

    Now, when I use this User Control in XAML I need to specify an xmlns for it (for example, "xmlns:pizza='clr-namesapce:global") but I'm not sure if it's possible to specify the global namespace in an xmlns element like this. I'm trying to do something like this in the XAML that referenes the User Control:


    <Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:pizza='assembly=global()'
        Title="Main Window">
    	
      <DockPanel Margin="8">
    <pizza:MyUserControl/> </DockPanel> </Window>
    Obviously, I can't specify, "global()" as shown above but I am looking for some syntax that allows me to map the "pizza:" XML namesapce to the global .NET namespace.

    Do I have to put my User Control in its own namespce? Is thee a way to accomplish this?

    Thanks...
    Saturday, April 4, 2009 7:58 PM

Answers

  • <Snip>

    In advance, there is no good reason not to use namespaces as these clearly separate components among libraries.

    Tomas

    Tomas,

    Thanks very much for the response. 

    Interestingly, you gave me pretty much the correct answer. Here is what works:

          xmlns:pizza="clr-namespace:;assembly="

    This uses the current assembly with no namespace, which is what I wanted. I am not sure what happens if you try a different assembly. I have not tried that yet.

    According to Microsof (see section, "Mapping to Current Assemblies") at http://msdn.microsoft.com/en-us/library/ms747086.aspx the above *should* be equivalent to this:

          xmlns:pizza="clr-namespace:"

    but it's not in this case. It seems you do need the 'assembly=' portion, despite what MSDN article says.

    Btw, as a minimalist, rather than using namespaces because "there is no good reason not to", I prefer to use them only when there is a good reason to do so.
    • Marked as answer by T. Baxter Sunday, April 5, 2009 5:44 AM
    • Edited by T. Baxter Sunday, April 5, 2009 5:52 AM corrected
    Sunday, April 5, 2009 5:33 AM

All replies

  • Hi,

    I don't think you can do what you wish. 
    As far as I succeded to use this definition in VS designer, compiler simply won't accept this.
        xmlns:pizza="clr-namespace:;assembly=WpfApplication1"

    Simply put your user-control into custom C# namespace.
    Designer will help you with XML to C# namespace mapping, later - it's easy and except XML prefix declaration, you won't see any namespace of XAML.
    In advance, there is no good reason not to use namespaces as these clearly separate components among libraries.

    Tomas


    Saturday, April 4, 2009 9:46 PM
  • <Snip>

    In advance, there is no good reason not to use namespaces as these clearly separate components among libraries.

    Tomas

    Tomas,

    Thanks very much for the response. 

    Interestingly, you gave me pretty much the correct answer. Here is what works:

          xmlns:pizza="clr-namespace:;assembly="

    This uses the current assembly with no namespace, which is what I wanted. I am not sure what happens if you try a different assembly. I have not tried that yet.

    According to Microsof (see section, "Mapping to Current Assemblies") at http://msdn.microsoft.com/en-us/library/ms747086.aspx the above *should* be equivalent to this:

          xmlns:pizza="clr-namespace:"

    but it's not in this case. It seems you do need the 'assembly=' portion, despite what MSDN article says.

    Btw, as a minimalist, rather than using namespaces because "there is no good reason not to", I prefer to use them only when there is a good reason to do so.
    • Marked as answer by T. Baxter Sunday, April 5, 2009 5:44 AM
    • Edited by T. Baxter Sunday, April 5, 2009 5:52 AM corrected
    Sunday, April 5, 2009 5:33 AM