Avalonia: Feature: alternative "using:" syntax to include namespace to XAML file

Created on 31 Jul 2020  路  10Comments  路  Source: AvaloniaUI/Avalonia

Currently Avalonia supports WPF'style namespaces in XAML files, using clr-namespace with assembly.
With that we need to provide long path to assemblies, if namespace isn't contained in current one.
UWP and XF supports another syntax, using "using:" keyword (for UWP it is only one available syntax).
With this we don't need to provide assembly, instead it will internally search for namespace in referenced projects and packages.

Proposal syntax:

<Styles xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="using:System"
        xmlns:controls="using:MyNamespaceInAnotherAssembly">
  <Style Selector="controls|MyControl" />
</Window>

UWP docs:
https://docs.microsoft.com/en-us/windows/uwp/xaml-platform/xaml-namespaces-and-namespace-mapping#mapping-custom-types-to-xaml-namespaces-and-prefixes

Xamarin Forms docs:
https://docs.microsoft.com/en-us/xamarin/xamarin-forms/xaml/namespaces#declaring-namespaces-for-types

xaml-compiler

Most helpful comment

I added this to the WinUI prototype Xaml compiler based on XamlX, and this is pretty easy to add to the XamlX library.

All 10 comments

In UWP it also works fine, when one namespace is used in multiple assemblies.
For instance, I can reference Microsoft.UI.Xaml package and use controls from its with namespace.
And then I can create new UserControl in local assembly but with same namespace - Microsoft.UI.Xaml.Controls, and reuse same namespace definition:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:controls="using:Microsoft.UI.Xaml.Controls">
  <controls:NavigationView >
    <controls:NavigationView.Content>
       <controls:CustomControl />
    </controls:NavigationView.Content>
   </controls:NavigationView >
</Page>
namespace Microsoft.UI.Xaml.Controls
{
    public class CustomControl : UserControl
    {
        public CustomControl()
        {
            Content = new Border
            {
                Background = new SolidColorBrush(Colors.Azure),
                Width = 100,
                Height = 100,
            };
        }
    }
}

yes, it should be exist, but that feature must be in the xaml-compiler, 'cause if that executes in the run-time would be very slow

I added this to the WinUI prototype Xaml compiler based on XamlX, and this is pretty easy to add to the XamlX library.

maybe additional to using keyword it is possible to add open as well?

xmlns:controls="open:Microsoft.UI.Xaml.Controls"

@FoggyFinder what are the semantics of the open syntax and how do they differ from the other syntax?

I propose open as a synonym to using with exactly the same meaning.

Context:

open is import declaration in F#.

Import Declarations: The open Keyword

C#

using System.IO;` 

F#

open System.IO

@jkoritzinsky is it possible to assume xmlsn:test="System.Diagnostic" was a clr namespace?

"using:" is well known UWP and XF syntax not only for C#, but also for F#/VB/C++ projects as well.
Need to mention, that "using:" isn't just a synonym for "clr-namespace:", but also provides some benefits with automatic assemblies lookup, when "clr-namespace" looks for current assembly or fails, if it wasn't provided explicitly.
On other hand we can just extend "clr-namespace:", so no need to maintain new syntax.

xmlsn:test="System.Diagnostic" could be parsed correctly in XamlX, but it will be completely new syntax for XAML language, and namespace name should be a valid URI, when "System.Diagnostic" doesn't looks like (not sure though).

"using:" is well known UWP and XF syntax not only for C#, but also for F#/VB/C++ projects as well.

Just because UWP doesn't care about anything except C#. To use it properly (release mode = .net native) you have to edit gatekeeper config manually. At least for F# for sure. I doubt the situation is better for VB (which is literally dead for Microsoft) and C++/CLI.

Also not all developers who use Avalonia have previous UWP/XF/WPF background, besides there are features that specific only to Avalonia already (at least I think so).

From the start Avalonia has better F# support than the rest of .net - ui - frameworks. Providing one alternative synonym for keyword doesn't sound like a big deal but it allows write more idiomatic code.

I love it. Done in latest 0.10-preview6.
image

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MarchingCube picture MarchingCube  路  4Comments

Urgau picture Urgau  路  3Comments

x2bool picture x2bool  路  4Comments

Suriman picture Suriman  路  3Comments

stdcall picture stdcall  路  4Comments