Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is xmlns in every WPF file?

Tags:

xml

wpf

xaml

What is xmlns?

What role does it play in an XAML file when we create a WPF project?

like image 548
Embedd_0913 Avatar asked Mar 10 '09 14:03

Embedd_0913


People also ask

What is xmlns in XAML file?

A XAML namespace is really an extension of the concept of an XML namespace. The techniques of specifying a XAML namespace rely on the XML namespace syntax, the convention of using URIs as namespace identifiers, using prefixes to provide a means to reference multiple namespaces from the same markup source, and so on.

What is xmlns used for?

An XML namespace is a collection of names that can be used as element or attribute names in an XML document. The namespace qualifies element names uniquely on the Web in order to avoid conflicts between elements with the same name.

Which attribute in WPF reference the XAML namespace?

A XAML namespace is a concept that expands on the definition of an XML namespace. Similar to an XML namespace, you can define a XAML namespace using an xmlns attribute in markup.

What is local in XAML?

local is an xml namespace. In this case "local" will be the alias for the namespace AskLocal. It will allow you to declare resources, controls, converters etc from the AskLocal namespace directly in your xaml by using <local:nameofyourcontrol></local:nameofyourcontrol>


2 Answers

xmlns is an XML, not necessarily XAML, construct which defines a namespace in which to resolve xml element names. Because it is defined without a qualifier, it is defining the default namespace by which an XML element name should be resolved.

In XAML you usually see the following entry. It defines the default namespace to be essentially WPF and all XML element names are hence resolved as WPF elements.

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

It's also common to see non-default namespaces such as the following.

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

This defines a qualified namespace for XAML specific elements. If you want an element or attribute name to be resolved within this namespace you should qualify it with x. For example

<StackPanel x:Name="foo" /> 

There are 2 name resolutions in this definition.

  1. StackPanel - Because it's an unqualified name, it will be resolved in the default namespace which is WPF
  2. x:Name - Name is qualified with x and will be resolved within the XAML document.
like image 100
JaredPar Avatar answered Oct 04 '22 05:10

JaredPar


And you use xmlns to get a reference to your own namespaces within your XAML as well. One of the first things I do when creating a new WPF project is to add a reference to the project namespace:

xmlns:local="clr-namespace:MyWpfProject" 

Now I have access to any classes I may create within my project (like IValueConverters and DataTemplateSelectors) by using the "local:" prefix

<local:BooleanToColorConverter x:Key="booleanToColorConverter" DefaultBrush="Green" HighlightBrush="Red" /> 

Of course, you don't have to use "local", you can name it whatever you want. And you can add references to any other namespace you need the same way.

like image 29
Joel Cochran Avatar answered Oct 04 '22 05:10

Joel Cochran