WPF Tutorial - Introduction to XAML

Introduction to XAML

1. Definition

A new "Extensible Application Markup Language" created for building application user interfaces, providing an easy-to-extend and locate syntax to define user interfaces separated from program business logic.

XAML is an extension of XML (Extensible Markup Language). XAML is a markup language used in WPF to define the user interface. It provides a declarative way to describe the appearance and behavior of the application. By using XAML, developers can better organize and maintain code, and achieve flexible and interactive user interfaces.

Example:

<Window x:Class="YourNamespace.YourWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:YourNamespace"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <!-- 在此处使用命名空间中的类型和资源 -->
        <local:CustomControl />
        <Button Content="Click Me" Click="Button_Click" />
        <TextBlock Text="{Binding Path=MyProperty}" />
        <sys:String>Hello, World!</sys:String>
    </Grid>
</Window>

2. Language specification

XAML is an XML-based language that follows or extends XML structural rules.

The object element syntax is the XAML markup syntax for instantiating a CLR class or structure by declaring an XML element. This syntax is similar to the element syntax of other markup languages ​​such as HTML. The object element syntax begins with an opening angle bracket (<), followed immediately by the type name of the class or structure being instantiated. Zero or more spaces may follow the type name, and zero or more properties may also be declared on the object element, using one or more spaces to separate each property name="value" pair. Finally, one of the following conditions must be met:

  • Elements and tags must end with a forward slash (/) followed by a closing angle bracket (>).
  • The opening tag must end with a closing angle bracket (>).

Specifically defined in the following two forms: <xxx />and<ABC ></ABC>

For example, the following example is the object element syntax

 <Button Content="Click Me" Click="Button_Click" />
 <TextBlock Text="{Binding Path=MyProperty}" ></TextBlock>

For other more detailed specifications, refer to the official website:
XAML Overview - WPF .NET
XAML Syntax Description - WPF .NET

3. Namespace

Like C#, xaml also has the concept of namespace, which is used to reference and use specific types and resources. By introducing the appropriate namespaces, the desired classes, structures, properties, and other definitions can be used in XAML.

xaml namespace definition

The syntax of the framework's own namespace:

There are some namespaces that come with wpf or the framework, which look like URLs in form, such as: http://schemas.microsoft.com/winfx/2006/xaml/presentation, and use the following format:

xmlns[:前缀名]=“命名空间描述”;

The namespace description is the content of that URL, and this URL-style namespace is a convention designed to ensure namespace uniqueness and provide an easy way to refer to and distinguish between different namespaces. When you define a namespace in XAML, you can use any valid URL-style string as a namespace identifier, as long as they are unique within your project.
Therefore, although these namespace URLs look like URL addresses, they are not connected to actual web resources and cannot be accessed on the web. They are only used as identifiers to ensure the uniqueness and distinguishability of the namespace.

Custom namespace or assembly mapping syntax:

xmlns[:必选前缀]="clr-namespace:[命名空间];assembly=[程序集名称]"

If used without a prefix, it comes from the default namespace, and other namespaces must be prefixed.

Common XAML namespaces

In WPF, common XAML namespaces include:

  1. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation": This is the default namespace, which defines the basic elements used to create a WPF user interface, such as windows, controls, layouts, and so on.
  2. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml": This namespace defines some common features and properties of XAML, such as aliases (x:) for XAML namespaces and resource references and definitions.
  3. xmlns:local="clr-namespace:YourNamespace": This namespace is used to reference custom types and resources in the current project. localis the prefix of the namespace, YourNamespacewhich is the namespace that contains custom types and resources.
  4. xmlns:sys="clr-namespace:System;assembly=mscorlib": This namespace is used to refer to system types and classes in the .NET Framework. sysis a namespace prefix that specifiesSystem;assembly=mscorlib the namespace in the assembly.mscorlibSystem

In addition to the above namespaces, you can also reference other custom namespaces, namespaces of third-party libraries, or specific WPF extension namespaces, depending on your project needs.

<Window x:Class="YourNamespace.YourWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:YourNamespace"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <!-- 在此处使用命名空间中的类型和资源 -->
        <local:CustomControl />
        <Button Content="Click Me" Click="Button_Click" />
        <TextBlock Text="{Binding Path=MyProperty}" />
        <sys:String>Hello, World!</sys:String>
    </Grid>
</Window>

expand

Define your custom namespace as:http://***

effect:

There are often many classes and controls in different namespaces in the project. If you use all namespaces to directly reference, you need a lot. This method can convert multiple namespaces into the same

In the framework project

Find the AssemblyInfo.cs file under Properties

insert image description here

Add namespace mapping transformation inside

[assembly:XmlnsDefinition("http://www.wpfcustomlibrary.com/controls", "WpfCustomControlLibrary1")]

insert image description here

Then add the namespace to the project you are using, and you can use it

   xmlns:control="http://www.wpfcustomlibrary.com/controls"

insert image description here

insert image description here

If there are multiple namespaces:

It can be mapped to the same namespace description

insert image description here
insert image description here

You can use the same namespace when using

insert image description here

In Net Standard project

Adding and using are the same, but the location of the AssemblyInfo.cs file is different. How to add WPF-related class libraries is directly under the directory. If ordinary class libraries may not have this file, you need to add it yourself.

insert image description here

Guess you like

Origin blog.csdn.net/qq_39427511/article/details/131252062