I've seen it a lot and I don't know what it means. I'd say it has something to do with namespaces? I've already googled it but it wasn't clear to me what it's purpose is.
Could anyone explain what it is and in which situations it is usually used?
Thanks
edit:
<Window x:Class="WpfApplication8.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
</Grid>
</Window>
But for example, in the above code, I am defining x
as an alias for the XAML namespace on the third line, although I am using that same x
right in the first line of code. How does this happen? It doesn't care for the order in which things appear?
edit2: Correct me if I'm wrong:
Window x:Class="WpfApplication8.MainWindow"
The above code will put a class derived from Window in the x:WpfApplication8.MainWindow namespace, while
Window x:Name="abc"
will put in the x namespace an instance of the Window class named abc. Is that right?
If I'm right, had I not used the x
alias, where would have both the class(first case) and instance(second) case have been put in? Nowhere, a bit like anonimous types? They are used but the place where they are is not defined?
You're correct, it's an XML namespace alias. If you take a look at the top of your xaml file, you'll find that it maps to the http://schemas.microsoft.com/winfx/2006/xaml
namespace. Using the prefix later on allows the xaml parser to locate classes defined in that namespace without you having to type out the entire thing. It's not unlike the using alias = very.long.namespace;
syntax in C#.
You normally need to set up a different alias for each namespace you intend to use in the xaml file. A common one when using PRISM is mapping cal
to clr-namespace:Microsoft.Practices.Composite.Presentation.Regions;assembly=Microsoft.Practices.Composite.Presentation
; you can then access the RegionManager
class defined in that CLR namespace by using cal:RegionManager
. When using Expression Blend, you'll often find that it adds xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
and xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
to allow for some of it's design time functionality (setting a width and height for the user control on the artboard).
Response to question edit: You might want to take a look at an XML Namespace tutorial, which is where xaml inherits this functionality from. In your example, you need to stop thinking of the lines as being individual instructions as they would be in procedural code and rather consider the element as a whole. What I'm trying to say is that because xmlns:x
appears in the Window
element, it is accessible to that element and every child element it contains, the actual line it appears on is not important as xaml is not executed from top to bottom like procedural code is.
Response to question edit 2: The x:Class attribute tells the compiler what the name of the partial class generated from the xaml file should be (if that is what you meant by "will put a class derived from Window...", then you're correct).
You're way off on the second part, the x:Name attribute tells the compiler to generate a field in the class containing a reference to the element to which the attribute is attached. What this means is that, in your example, your code behind file will be able to use this.abc
to refer to the Window element defined in the xaml markup.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With