Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clr namespace mapping to default xaml namespaces

Tags:

c#

.net

wpf

xaml

I'm a bit tired of having to declare an xmlns in every xaml file and having to use prefixes for my custom controls. Is it possible to map a clr namespace to "http://schemas.microsoft.com/winfx/2006/xaml/presentation"?

I tried the following in my AssemblyInfo.cs:

[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation","MyOwnNamespace")]

but this doesn't seem to work. I still get a compile error like:

The tag 'MyCustomControl' does not exist in XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation'.

Note: My controls are within the same assembly (I have a single assembly).

like image 478
Eren Ersönmez Avatar asked May 11 '12 11:05

Eren Ersönmez


People also ask

What does CLR namespace mean?

clr-namespace: The CLR namespace declared within the assembly that contains the public types to expose as elements. assembly= The assembly that contains some or all of the referenced CLR namespace. This value is typically just the name of the assembly, not the path, and does not include the extension (such as .

What is XAML namespace declaration?

A XAML namespace is a specialized XML namespace, just as XAML is a specialized form of XML and uses the basic XML form for its markup. In markup, you declare a XAML namespace and its mapping through an xmlns attribute applied to an element.

Which attribute in WPF reference the XAML namespace?

The xmlns attribute specifically indicates the default XAML namespace. Within the default XAML namespace, object elements in the markup can be specified without a prefix.


1 Answers

  1. Unfortunately, you cannot use controls mapped to a Xaml namespace defined by XmlnsDefinition, if the controls are defined in the same assembly. Different assemblies work fine though. You'll have to use the clr-namespace definition for this.

  2. Why would you want to add your controls to the Xaml default namespace? Don't do this. It's like using the System namespace for your classes because you don't want to add using directives for their namespaces.

like image 165
Botz3000 Avatar answered Oct 10 '22 21:10

Botz3000