locked
what is the namespace in XMAL in WPF ?? RRS feed

  • Question

  • hi all , 

    pls can anyone explain me more esily what is mean by" X: " why they are mapping namespace in to X ?

    reference " http://msdn.microsoft.com/en-us/library/ms747086.aspx"

     
      The WPF and XAML Namespace Declarations
    <!---->

    Within the namespace declarations in the root tag of many Extensible Application Markup Language (XAML) files, you will see that there are two XML namespace declarations. The first declaration maps the overall Windows Presentation Foundation (WPF) namespace as the default:

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    The second declaration maps a separate Extensible Application Markup Language (XAML) namespace, mapping it (typically) to the x: prefix.

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    The relationship between these declarations is that the x: prefix mapping supports the intrinsics that are part of the XAML language definition, and WPF is one implementation that uses XAML as a language and defines a vocabulary of its objects for XAML. Because the WPF vocabulary's usages will be far more common than the XAML intrinsics usages, the WPF vocabulary is mapped as the default.

    The x: prefix convention for mapping the XAML language intrinsics support is followed by project templates, sample code, and the documentation of language features within this SDK. The XAML namespace defines many commonly-used features that are necessary even for basic WPF applications. For instance, in order to join any code-behind to a XAML file through a partial class, you must name that class as the x:Class attribute in the root element of the relevant XAML file. Or, any element as defined in a XAML page that you wish to access as a keyed resource should have the x:Key attribute set on the element in question. For more information on these and other aspects of XAML see XAML Overview or XAML Syntax Terminology.

    Tuesday, January 5, 2010 5:12 AM

Answers

  • x: is a namespace alias. It is used to provide access to classes within the specified namespace.

    For example you will see

    <TextBlock x:Name="Foo" />

    The x:Name attribute is defined in the namespace that is behind the http://schemas.microsoft.com/winfx/2006/xaml namespace. Notice I said "behind the .... namespace." This is because in the case of x: it is a bit more complicated how the URL specified is actually being turned into an assembly reference (I can explain this further if you are interested but you mentioned you were looking for a simple asnwer so I will skip this for now).

    The clear explanation is when you want to reference your own controls in your own namespace you need to provide an alias to your namespace. Something like this


    xmlns:MyNamespace="clr-namespace:Foo"


    Where Foo is:
    namespace Foo
    {
    
    public class bar :UserControl
    {
    }
    
    }


    Then you can reference the Bar user control by doing this

    <Foo:Bar />


    where Foo is the namespace alias and Bar is the control from within the namespace you are looking for.



    Hopefully that clarfies things for you

    • Edited by Foovanadil Tuesday, January 5, 2010 5:25 AM Edit code snippet
    • Proposed as answer by Foovanadil Tuesday, January 5, 2010 6:19 PM
    • Marked as answer by aniruddha84 Tuesday, January 19, 2010 9:01 AM
    Tuesday, January 5, 2010 5:25 AM

All replies

  • x: is a namespace alias. It is used to provide access to classes within the specified namespace.

    For example you will see

    <TextBlock x:Name="Foo" />

    The x:Name attribute is defined in the namespace that is behind the http://schemas.microsoft.com/winfx/2006/xaml namespace. Notice I said "behind the .... namespace." This is because in the case of x: it is a bit more complicated how the URL specified is actually being turned into an assembly reference (I can explain this further if you are interested but you mentioned you were looking for a simple asnwer so I will skip this for now).

    The clear explanation is when you want to reference your own controls in your own namespace you need to provide an alias to your namespace. Something like this


    xmlns:MyNamespace="clr-namespace:Foo"


    Where Foo is:
    namespace Foo
    {
    
    public class bar :UserControl
    {
    }
    
    }


    Then you can reference the Bar user control by doing this

    <Foo:Bar />


    where Foo is the namespace alias and Bar is the control from within the namespace you are looking for.



    Hopefully that clarfies things for you

    • Edited by Foovanadil Tuesday, January 5, 2010 5:25 AM Edit code snippet
    • Proposed as answer by Foovanadil Tuesday, January 5, 2010 6:19 PM
    • Marked as answer by aniruddha84 Tuesday, January 19, 2010 9:01 AM
    Tuesday, January 5, 2010 5:25 AM
  • hi Foovanadil ,

    thx lot .. u solve my problm ..


    (I can explain this further if you are interested but you mentioned you were looking for a simple asnwer so I will skip this for now).


    pls go ahead .. i m interested ..



    Tuesday, January 5, 2010 5:45 AM
  • In the case of the URL aliases there are actually two different "aliases" being used.

    First xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    in this portion the x is the namespace alias. This means anywhere in your XAML that you want to reference types in the specified namespace you will prefix the type with x:

    Like I showed above, most often you will use your own clr namespace as the right side of the equals for an alias and that will be it.


    In the case of the WPF framework they are further adding an alias for the namespace itself (they are doing this so they can coalesce multiple physical namespaces into one namespace alias)

    So for : xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml/presentation "

    The url specified here isn't really pointing to any physical URL it is simply an arbitrary piece of text that the WPF team decided to use to wrap core WPF assemblies into one place. This URL is specified in the AssemblyInfo.cs file for each of the different assemblies they want to coalesce.

    This allows the XAML parser to find the types needed across multiple namespaces.

    If you load the WindowsBase assembly into Reflector you will see these XmlnsDefinition specified in the AssemblyInfo like this

    ...
    [assembly: XmlnsDefinition ("http://schemas.microsoft.com/netfx/2007/xaml/presentation" , "System.Windows" )] [assembly: XmlnsDefinition ("http://schemas.microsoft.com/netfx/2007/xaml/presentation" , "System.Windows.Input" )] [assembly: XmlnsDefinition ("http://schemas.microsoft.com/netfx/2007/xaml/presentation" , "System.Windows.Media" )]
    [assembly: XmlnsDefinition ("http://schemas.microsoft.com/netfx/2007/xaml/presentation" , "System.Diagnostics" )]
    ...

    You can see how there are multiple physical namespaces that are all pointing to the same "URL". This creates a single alias that can be used in XAML to access to any public type in any of the multiple namespaces.

    You can do this same thing for your own assemblies if you want to. I use this technique on my projects to conditionally include mock data assemblies for design time support (see here for what I mean: http://www.removingalldoubt.com/PermaLink.aspx/defa2a7d-b1e9-49eb-b8c8-438348be8d18 )



    Hopefully that makes sense?



    Tuesday, January 5, 2010 6:37 PM