WPF快速学习笔记三 x名称空间

本文深入探讨了XAML中的高级特性,包括x:Class、x:Static等属性的作用及使用方法,并通过具体示例展示了如何利用这些特性提高开发效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、x出现的方式有三种

                 1、标签扩展 :x:Arry 、x:Null、x:Static、x:Type

                2、XAML指令元素: x:Code, x:XData

                 3、Attribute: x:Class, x:ClassModifier, x:FieldModifier, x:Key, x:Name, x: Shared, X:Subclass,

                                         x:TypeArguments, x:Uid

                x:Class:告诉XAML编译器将XAML标签的编译结果与后台制定的类合并,这个类必须使用partial关键字。

                 x:ClassModifier:访问级别,是否是公有还是私有.

                 x:Name: XAML声明的对象的名称。

                  x:FieldModifier: 访问级别 是否是公有还是私有 也就是访问域。

                  x:Key: 主要用于检索的值

                 x:Shared: 资源是否共享。

                  x:Null: 清除一些设置,比如全局style设置的Button样式,某个Button不想用,可以使用
                             Style=”{x:Null}”.

                 1、x:Arry  该扩展标签是通过它的Items属性向使用者实例化ArrayList类,ArrayList内成员的类型
                             由x:Array 的Type指明 实例:

  源码

<ListBox>
            <ListBox.ItemsSource>
                <x:Array Type="sys:String">
                    <sys:String>1
                    </sys:String>
                    <sys:String>2
                    </sys:String>
                    <sys:String>3
                    </sys:String>
                </x:Array>

            </ListBox.ItemsSource>
        </ListBox>

(注意 sys:String 需要导入 程序集才能用    xmlns:sys="clr-namespace:System;assembly=mscorlib" )

                   x:Static  静态成员 跟普通的static一样 能在整个程序使用 实例

  源码

  在cs 程序里声明一个静态变量 test 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace firstApplicaton
{
    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    public partial class Window1 : Window
    {
        //在这申明静态变量
        public static string test="test";
        public Window1()
        {
            InitializeComponent();

        }

     }
}

      源码  前台获取静态的 变量的test的值并显示在 界面的textbox里面

  <TextBox x:Name="b" Text="{x:Static a:Window1.test}"

(注意 :a 是 导入的命名空间  xmlns:a="clr-namespace:firstApplicaton" )

              2、x:Type   数据类型 这个一般我们很少用到  但是有可能在某个时候还是会有点用处。

自定义一个button  如果该button click 事件 窗体的 类型是 window 类型 那就以模式串口显示该 窗体。

class MyButton:Button
{
public Type UserWindowType {get;set; }
protected overridevoid OnClick(
{
base.OnClick();
Window win = Activator.CreateInstance(this.UserWindowType)as Window;
if(win != null)
win.ShowDialog();
}

window1

<Window x:Class="WpfApplication.Window1"

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

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

Title=
"Window1" Width="200" Height="170">

<StackPanel Background="LightBlue">

<TextBox />

<TextBox />

<Button Content="确认" />

</StackPanel>

</Window>

window2

<Window x:Class="WpfApplication.Window2"

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

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

Title=
"Window2" Width="300" Height="300">

<StackPanel>

<local:MyButton Content="Show"
UserWindowType=
"{x:Type TypeName=local:Window1}" />

</StackPanel>

</Window>

单击window2的 show  将以模式窗口的形式 来显示 window1

        3、 x:Null 表示空值 有时候在在给attribute 赋值的时候就可以用该 x:Null来 赋予空值。

   <Window.Resources>
        <Style TargetType="Label"> <!--默认所有的Label的背景色为红色-->
            <Setter Property="Background" Value="red"/>
        </Style>
    </Window.Resources>

<Label  Content="ceshi" Style="{x:null}"/> <!--该语句去除了red背景的风格-->

        4、x:Code 用于在xaml中写入C#代码 这个东西也非常的好用

         5、x:data

    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值