Using Xml Namespaces to optimize your imports in Xamarin.Forms

Learn how to optimize your xmlns imports with this new feature available in Xamarin.Forms 3.5

Using Xml Namespaces to optimize your imports in Xamarin.Forms

Hi techies, today we're going to review a little, but important XAML feature in Xamarin.Forms, that we can use to optimize importing of classes, controls and effects in our XAML code. This feature has just seen the light with the release 3.5 of Xamarin.Forms.

If we have a good time enjoying creating applications with Xamarin.Forms, we will probably have created our own libraries with controls, effects, behaviours, value-converters, even any class helper.

Sample Project with reusable components

And surely you have noticed how annoying importing is, either, your own libraries or the libraries available in NuGet. So I always end up navigating to the documentation in Github to find the xmlns import to copy and paste it.

Traditional Xmlns

Yes, there are so many prefixes and namespaces to include. Can you imagine writing it by hand? At least Visual Studio allows us to autocomplete it. But keep calm and relax, we would not have gotten here if we did not have a solution for this. Xamarin.Forms 3.5 allows us to define XML namespaces in the same style as Xamarin.Forms core libraries do.

xaml xmlns

This is possible because of the attribute used in the Xamarin.Forms project (XmlnsDefinition) is public now and we can use it in our own libraries. This allows us to transform our namespaces entanglement into this:

Using our own xmlns


Amazing! Right? Now I can use all my classes using my techies prefix.

How do I start using it in my library?

The XmlnsDefinition class is an assembly level attribute, its perfect location would be the AssemblyInfo.cs file, if we are using a .NET Standard library, you have noticed that it does not have a file of this type. You can add a file with the same name for clarity or place it at the assembly level (outside the namespace declaration) in any code file.

AssemblyInfo.cs

For example, I want to map my namespaces about Controls and Converters so I can expose them using somostechies.com/techies, for this, my AssemblyInfo.cs file should look like this:

Finally, in my Xamarin.Forms project, in this example, is called TechiesNS, I can import my library using a single namespace in the same style as the core libraries of Xamarin.Forms.

Now, when compiling, our old enemy, the Linker, will remove our code file containing the mappings, so they will have errors that essentially try to tell us that they can not find the types they are using within our .dll.

This can be solved by configuring the Linker in our .csproj or by adding a dummy class as we can see below so that the Linker does not eliminate the mappings that we want so much.

Dummy class and reference in order to preserve mapping code

Remember that you need to define this mapping in your library code, we can´t do this in our application code, by now we have this limitation.

Don´t wait! Update your apps to Xamarin.Forms 3.5 today!

if you like to review the code yourself, the sample project used in this post is available at Github https://github.com/jesulink2514/TechiesNS.