XAML 的新增内容.x: 和 :x 是什么意思?



我是XAML的新手。我想知道所有的x:和:x都是关于什么的。有关 XAML 的教程没有解释这一点(或者我还没有读够(。

例如:

<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>

x在这些行中是什么意思?

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

这定义了一个命名空间映射,即前缀x映射到http://schemas.microsoft.com/winfx/2006/xaml命名空间:

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

有关 XAML 命名空间和命名空间映射的详细信息,请参阅 MSDN。

WPF XAML 的 XAML 命名空间和命名空间映射:https://learn.microsoft.com/en-us/dotnet/framework/wpf/advanced/xaml-namespaces-and-namespace-mapping-for-wpf-xaml

如果要执行以下操作,可以将x更改为其他内容:

X:XAML 中的含义

如前所述,它只是一个映射到命名空间的前缀,以便能够在 XAML 标记中使用命名空间中定义的类型或属性。

最新更新