.Net MAUI

Simplifying Implicit and Global XML Namespaces in .NET 10 for .NET MAUI

I know, I know… as developers we’re always happy when we get the chance to save code without affecting the result 😎. In this article, we’re going to explore the new experience that allows us to add XML-namespaces in a much cleaner and reduced way — a feature introduced in .NET 10 Preview 5.

What is it about?

Basically, it’s a new namespace (I personally call it “the parent” 😝) that contains several of the namespaces needed for your XAML to work. This way, it lets you remove almost all the repetitive xmlns lines placed at the beginning of each XAML file—the ones you used to copy and paste into every file… even though they were always the same.

Let’s take a look at the changes

Project-wide “global” namespaces

➖ http://schemas.microsoft.com/dotnet/maui/global is the new xmlns, which allows you to aggregate several under the same namespace. By default, it contains:

🔹 The MAUI xmlns (source-generated)

🔹{YourNamespace}

🔹{YourNamespace}.Pages

Let’s see an example of how to use it!

Before (.NET 8 style)

After (.NET 10 Preview 5)

Brief summary of the code:

  • xmlns:models or xmlns:controls are no longer necessary because they are declared globally in a GlobalXmlns.cs file.

  • In the previous version, to call TagView you needed the prefixes:

  • Now you can do it directly, without adding them:


➖ Backward compatible

Even though we now have this simpler way of adding namespaces, the xmlns: declarations you already have with the old format will still work without any issues. However, I recommend keeping your code updated with the new approach as soon as you can — it’s always worth it! 🤓

In which cases should I still use explicit xmlns:?

Use them when you need to disambiguate duplicate type names. Imagine a case where two different namespaces contain types with the same name (for example, two classes called Label in different libraries). In that scenario, prefixes (controls:Label, custom:Label) will help you clearly distinguish them.


➖ Implicit default namespace (opt-in)

When opting in, the compiler automatically injects the root namespace http://schemas.microsoft.com/dotnet/2021/maui, which means you can remove the root xmlns and xmlns:x lines.

Let’s see an example:

Looks like magic, right? 😆 Even cleaner — here you don’t need to add xmlns or xmlns:x because they’re already implicitly included in the global namespace.


How to try the experience?

Of course, we can’t stay behind my friend! 😎 Take a project as your example and follow these steps:

  1. Upgrade the project to net10.0- target frameworks.

2. Create a GlobalXmlns.cs file at the project level to register your CLR namespaces so they’re available globally in XAML, as in the following example.

3. Delete redundant xmlns: lines and prefixes from your XAML.

The other option is that if you prefer to continue using xmlns prefixes in your XAML, you can define default prefixes in the GlobalXmlns.cs file.

Done! 😎 From now on, all that’s left is to apply it to your projects and keep everything up to date.

🙌 Thanks for reading my article

If you have any questions, feel free to leave them in the comments. 💚💕

See you next time! 🙋‍♀️


Spanish article

References

This article was based on the official documentation.

Tagged ,

Leave a Reply

Your email address will not be published. Required fields are marked *