standbyfor
standbyfor

Reputation: 173

XAML (WPF) Namespace?

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?

Upvotes: 1

Views: 281

Answers (2)

John Bowen
John Bowen

Reputation: 24453

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"

Upvotes: 0

Patrick Hofman
Patrick Hofman

Reputation: 156978

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.

Upvotes: 2

Related Questions