7

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).

1 Answer 1

9
  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.

4
  • 4
    Well, if I had to put "local:" in front of every variable that references my classes, and not were able to add the using so simply (thanks to VS resolve->add using...), then I'd also be asking you how I can include my classes in the System namespace :) May 11, 2012 at 11:20
  • @ErenErsönmez ok, Xaml certainly isn't as user-friendly as the C# editor :) Is using a shorter prefix than local: not an option? I know i have been doing this at some point and used l: instead because that long prefix looked ugly.
    – Botz3000
    May 11, 2012 at 11:29
  • Yes, l: would be more convenient than local: for sure. I might use that. Thanks. May 11, 2012 at 11:40
  • I'd agree on #2 as a best practice, but depending how you're feeling, you wouldn't have to use any xmlns declarations on your controls if you do that. Personal opinion but just makes it transparent from which namespace controls are being referenced from.
    – Kcvin
    Aug 19, 2015 at 23:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.