XAML Essentials
Importing a namespace
The semantic of xmlns:x
basically means importing a namespace as an alias named x
. Then we can use :
to access the members of the namespace. x:Class
binds the code behind(.axaml.cs
) the AXAML(not the ViewModel!).
xml
<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="AvaloniaApplication.App"
xmlns:local="using:AvaloniaApplication"
RequestedThemeVariant="Default">
<!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. -->
</Application>
1
2
3
4
5
6
7
2
3
4
5
6
7
Spreading Namespace
The default namespace is imported without name, which mean its members are imported globally in this AXAML file, can be accessed them without any prefix. Generally the default is avalonia namespace which contains built-in components like Button
, TextBlock
.
xml
<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="AvaloniaApplication.App"
xmlns:local="using:AvaloniaApplication"
RequestedThemeVariant="Default">
<!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. -->
</Application>
1
2
3
4
5
6
7
2
3
4
5
6
7
Import from Project Namespace
using:<namespace>
imports the namespace from specific project with the alias name local
xml
<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="AvaloniaApplication.App"
xmlns:local="using:AvaloniaApplication"
RequestedThemeVariant="Default">
<!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. -->
<Application.DataTemplates>
<local:ViewLocator/> <!-- use `local` here to access types from it -->
</Application.DataTemplates>
</Application>
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11