Allow registering xmlns prefixes using the `using:[namespace]` syntax
- Dominant language
- C#
- Stars
- 7.7k
- Forks
- 1.3k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 61
Description
It would be great if we could extend the syntax for registering xmlns prefixes to adopt the pattern used by UWP ( [documented here](https://docs.microsoft.com/en-us/windows/uwp/xaml-platform/xaml-namespaces-and-namespace-mapping#mapping-custom-types-to-xaml-namespaces-and-prefixes) ), and achieve a little more consistency between the two XAML Flavors.
Currently we have two options:
1. Use the namespace and assembly name with the following syntax:
```xml
```
2. The second option is to register a prefix in the assembly itself using the xmlnsprefix attribute:
```cs
[assembly: XmlnsPrefix("http://schemas.dotMorten.com/2018", "custom")]
[assembly: XmlnsDefinition("http://schemas.dotMorten.com/2018", "MyClassNamespace")]
```
```xml
```
The first one is rather verbose and tied to a single specific assembly, and the second one has to be done at the assembly level, which isn't possible if you don't own the assembly. Secondly it's yet-another-difference from UWP XAML, which could easily be remedied with the following syntax:
```xml
```
I made a tiny prototype in System.Xaml to accomplish this. While it's far from the most efficient way, it proves that it can be done in WPF, by adding the following to XamlSchemeContext.cs in the [GetXamlNamespace](https://github.com/dotnet/wpf/blob/c8184619f08a7396c8d229c098ffcee859dff36f/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlSchemaContext.cs#L963) method:
```cs
if (xmlns.StartsWith("using:"))
{
var ns = xmlns.Substring(6);
foreach(var a in AppDomain.CurrentDomain.GetAssemblies())
{
if (a.ExportedTypes.Where(t => t.GetTypeInfo().Namespace == ns).Any())
{
if(xamlNamespace == null)
xamlNamespace = new XamlNamespace(this);
xamlNamespace.AddAssemblyNamespacePair(new AssemblyNamespacePair(a, ns));
continue;
}
}
if (xamlNamespace != null)
return xamlNamespace;
}
```
([also I got a branch with this change here](https://github.com/dotMorten/wpf/tree/xmlns_using))
There's obviously a performance issue with this approach, as it requires iterating all loaded assemblies each time the namespace declaration is encountered, but considering assemblies getting loaded and unloaded is already being tracked, I think it should be possible to piggy-back on that and maintain a cache of namespaces in each assembly.
Screenshot from this implementation in use:

I'd be happy to volunteer for this work, but would appreciate a few tips wrt ensuring performance and caching of the lookups.
#60
Contributor guide
Assessment
This issue has not been assessed yet.