4
votes

I'm new to XAML. And I wonder what all the x: and :x are all about. Tutorials about XAML does not explain this (or I haven't read enough yet).

For example:

<Window x:Class="WpfTutorialSamples.WPF_Application.ResourceSample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="ResourceSample" Height="150" Width="350">
    <Window.Resources>
        <sys:String x:Key="strHelloWorld">Hello, world!</sys:String>
    </Window.Resources>
    <StackPanel Margin="10">
        <TextBlock Text="{StaticResource strHelloWorld}" FontSize="56" />
        <TextBlock>Just another "<TextBlock Text="{StaticResource strHelloWorld}" />" example, but with resources!</TextBlock>
    </StackPanel>
</Window>

What does x mean in these lines?

  • Window x:Class="WpfTutorialSamples.WPF_Application.ResourceSample"
  • xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" sys:String
  • x:Key="strHelloWorld">Hello, world!
1

1 Answers

9
votes

This defines a namespace mapping, i.e. the prefix x maps to the http://schemas.microsoft.com/winfx/2006/xaml namespace:

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Please refer to MSDN for more information about XAML namespaces and namespace mappings.

XAML Namespaces and Namespace Mapping for WPF XAML: https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/xaml-namespaces-and-namespace-mapping-for-wpf-xaml

You might change x into something else if you want to:

x: meaning in xaml

As mentioned, it is just a prefix that is mapped to a namespace in order for you to be able to use a type or attribute that is defined in the namespace in your XAML markup.