1

I have been trying to read up on XAML namespace and the use of xlmnr and it has been kind of fuzzy. Either it is too technical, or too simplistic.

My question is a little similar to a question asked here, but my question has more to do with the x part attached to it.

So:

  1. Does the xmlns:x, mean a secondary namespace? i.e. the non-default one? Can I have more than one, and if so what order does the search for the right class go in? This of course assumes that xmlns is the default one.
  2. What about the meaning of and difference of attaching x:name as opposed to name to a tag?

Edit:

Turns out, I think I completely misunderstood it. There is no search hiearchy like C# using statement, or java's import. The xmlns:<name> is more like a way to define a name that you can access a whole tree of classes. The x on the other hand is a conventional way to define XMAL related stuff, but is not a requirement.

Can anyone confirm?

Community
  • 1
  • 1
standbyfor
  • 173
  • 1
  • 1
  • 5

2 Answers2

2

The use of XML namespaces in XAML is necessary because of the underlying XML technology used.

xmlns:x indeed creates a second namespace named x. You can reference attributes, etc from it using x:....

If you had simply use name instead of x:name it would have referenced the default namespace.

You can have as much namespaces declared in your XAML as needed.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • So could I have defined a `xmlns:fifafoom` and used a `fifafoom:name`? I see the `x`used a lot, but it sounds like it could have been anything? If I can name my own, what order does it search in? Finally, can I name two things the same in different namespaces? Example `x:name="fig"` and `name="fig"`. – standbyfor May 06 '14 at 14:01
  • @standbyfor: That is perfectly fine. – Patrick Hofman May 06 '14 at 14:02
0

The standard x namespace exposes common XAML features - basically a mapping of various things that are implemented in code to give them meaning in the XAML context. In the case of x:Name (not x:name - case matters) the XAML compilation process creates a code-behind field based on the x:Name value. The non-x Name property is an attribute representing the Name property on the WPF FrameworkElement base class, which in most cases works the same way as setting the x:Name, and you can't assign both on the same element. See this question for more info.

To the first part of your question: you can change the x to whatever you want, but shouldn't to maintain consistency, and can also (and will in practice) add other xmlns: declarations, primarily to access additional feature implemented in code. For example, if you work in Blend you will often see a xmlns:d added which contains a bunch of designer specific properties. Any code that you need to reference, like data types, converters, etc. will generally use an xmlns: with clr-namespace and assembly specified to map to the .NET namespace in the code: i.e. xmlns:local="clr-namespace:WpfApplication1"

Community
  • 1
  • 1
John Bowen
  • 24,213
  • 4
  • 58
  • 56
  • So the `x` namespace is a built-in namespace which exposes common XAML features? Or is it just a name commonly used (maybe by convention) for secondary namespace to search in? Does it search multiple namespaces when I reference something, or do I need to be specific which namespace I am referring to? – standbyfor May 06 '14 at 15:24
  • x is used by convention and added automatically by the templates for all XAML files so unless you write one from scratch it's always there. It's a URL mapping (to http://schemas.microsoft.com/winfx/2006/xaml) so can actually cover multiple CLR namespaces. There's nothing related to searching in any of this. If you want to use the Static markup extension (which is defined in code included in http://schemas.microsoft.com/winfx/2006/xaml) you need to use {x:Static}. – John Bowen May 06 '14 at 15:45