X名称空间详解

1.x:Name的作用

告诉编译器,为标签处理为这个标签生成对应实例外,还要为实例声明一个引用变量,变量名就是x:Name的值;如果xaml标签所对应对象存在Name属性,也会想值赋值给Name属性;示例如下:

    <StackPanel>
        <TextBox x:Name="textBox" Margin="5"/>
        <Button Content="OK" Margin="5" Click="Button_Click"/>
    </StackPanel>


       private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(textBox.Name))
            {
                textBox.Text = "no name";
            }
            else
            {
                textBox.Text = textBox.Name;
            }
        }

2.x:FileldModifier  访问级别

    <StackPanel>
        <TextBox x:Name="textBox1" x:FieldModifier="public" Margin="5"/>
        <TextBox x:Name="textBox2" x:FieldModifier="public" Margin="5"/>
        <TextBox x:Name="textBox3" Margin="5"/>
    </StackPanel>

3.x:key  静态资源的访问  需要引用mscorlib命名空间,然后添加资源文件;

<Window x:Class="HelloWpf.MainWindow"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <sys:String x:Key="myString">Hello Wpf Resource</sys:String>
    </Window.Resources>

访问如下:

 <StackPanel Background="Gray">
        <TextBox Text="{StaticResource ResourceKey=myString}" Margin="5"/>
        <TextBox x:Name="textBox1" Margin="5"/>
        <Button Content="Show" Click="Button_Click" Margin="5"/>
    </StackPanel>

  C#中也能访问:
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string str = this.FindResource("myString") as string;
            this.textBox1.Text = str;
        }

4.x:Shared  :和x:key配合使用,如果x:Shared的值为true,多个地方引用得到的是同一个对象,如果是false,则得到的是这个对象的副本,默认为true;

5.x:Type  :有时需要将一个Type当值传入属性中,例如,把本地的MyWindow传入Button中的UserWindowType属性中

  <StackPanel>
        <local:MyButton Content="Show" UserWindowType="{x:Type local:MyWindow}" Margin="5"/>
    </StackPanel>
 class MyButton:Button
    {
        public Type UserWindowType { get; set; }

        protected override void OnClick()
        {
            base.OnClick();
            Window win = Activator.CreateInstance(this.UserWindowType) as Window;
            if (win != null)
            {
                win.ShowDialog();
            }
        }
    }

 6.x:Array 使用标签声明语法使用x:Array

<ListBox Margin="5">
        <ListBox.ItemsSource>
            <x:Array Type="sys:String">
                <sys:String>Tim</sys:String>
                <sys:String>Tom</sys:String>
                <sys:String>Victor</sys:String>
            </x:Array>
        </ListBox.ItemsSource>
    </ListBox>

7.x:static 因为在xaml中不能编写逻辑代码,所以只能访问数据类型的属性或者字段

namespace HelloWpf
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public static string WindowTitle = "山高月小";
        public static string ShowText { get { return "水落石出"; } }
        public MainWindow()
        {
            InitializeComponent();
        }

    }
}
<Window x:Class="HelloWpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:local="clr-namespace:HelloWpf"
        mc:Ignorable="d"
        Title = "{x:Static local:MainWindow.WindowTitle}" Height="450" Width="800">
</Window>

8.x:XData  就是为了提供xml化的数据;

posted @ 2024-08-22 19:26  你好呀嗯嗯  阅读(27)  评论(0)    收藏  举报