Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New to XAML. What does the x: and :x mean?

Tags:

wpf

xaml

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!
like image 682
Eerik Sweden Avatar asked Aug 22 '17 17:08

Eerik Sweden


People also ask

What does X mean in XAML?

x:Code is a directive element defined in XAML. An x:Code directive element can contain inline programming code. The code that is defined inline can interact with the XAML on the same page. The following example illustrates inline C# code.

What is X key in XAML?

The code equivalent of specifying x:Key is the key that is used for the underlying IDictionary. For example, an x:Key that is applied in markup for a resource in WPF is equivalent to the value of the key parameter of ResourceDictionary. Add when you add the resource to a WPF ResourceDictionary in code.

What is X type in WPF?

WPF supports techniques that enable specifying the value of some properties of type Type without requiring an x:Type markup extension usage. Instead, you can specify the value as a string that names the type. Examples of this are ControlTemplate.

What is xmlns X?

The xmlns:x attribute indicates an additional XAML namespace, which maps the XAML language namespace http://schemas.microsoft.com/winfx/2006/xaml. This usage of xmlns to define a scope for usage and mapping of a name scope is consistent with the XML 1.0 specification.


1 Answers

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.

like image 99
mm8 Avatar answered Nov 01 '22 10:11

mm8