《深入浅出WPF》学习笔记三.x命名空间以及常见属性

《深入浅出WPF》学习笔记三.x命名空间以及常见属性

X命名空间的由来和作用

xaml:eXtensible Application Markup Language的英文缩写(可扩展应用程序标记语言);

声明

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

使用

x:Class="WpfApp10.MainWindow"

xmlns:xml namespace的缩写

http://schemas.microsoft.com/winfx/2006/xaml不是一个类库,而是代表着wpf用于解析分析xaml代码所需要的一系列的的类库。

x:Class

x:Class="WpfApp10.MainWindow"

使用指明当前的xaml代码最后与后台那个代码进行合并(MainWindow.xaml.cs);

x:ClassModifier

用于指明xmal的声明权限,需要与后台合并代码的声明一致,否则会报错;

比如

在MainWindow.xaml中

cs 复制代码
x:ClassModifier="public"

则在MainWindow.xaml.cs中需要声明

cs 复制代码
    public partial class MainWindow : Window

x:ClassModifier不写,则默认声明为public

x:Name

作用:

1.在x命名空间声明一个对象,相当于Button btn1=new Button();

2.可以在后台代码中可以直接访问已经声明的对象;

cs 复制代码
        <Button Name="btn1" Content="点击一下" Width="100" Height="80" Click="btn1_Click"></Button>
cs 复制代码
        private void btn1_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(this.btn1.Name);
        }

x:Name和Name的区别

Name:是FrameworkElement的属性,在此情况性Name效果与x:Name一致

如果对象非FrameworkElement对象,则必须使用x:Name

cs 复制代码
<Window x:Class="WpfApp10.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:local="clr-namespace:WpfApp10"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Button Name="btn1" Width="100" Height="80" Click="btn1_Click">
            <Button.Content>
                <local:Student x:Name="stu1"></local:Student>
            </Button.Content>
        </Button>

    </Grid>
</Window>
cs 复制代码
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;

namespace WpfApp10
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            stu1.Name = "stuName";
        }

        private void btn1_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(this.stu1.Name);
        }
    }

    public class Student
    {
        public string Name { get; set; }
    }
}

x:FieldModifier

控制字段的访问级别,如下设置userControl1中的textbox访问级别为x:FieldModifier="private"

则主页面访问会报错

严重性 代码 说明 项目 文件 行 禁止显示状态

错误(活动) CS0122 "UserControl1.tb1"不可访问,因为它具有一定的保护级别 WpfApp10

cs 复制代码
<Window x:Class="WpfApp10.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:local="clr-namespace:WpfApp10"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
            <local:UserControl1 x:Name="uc1"></local:UserControl1>
        <Button Name="btn1" Width="100" Height="40" Content="改变文字" Click="btn1_Click">
        </Button>
        </StackPanel>

    </Grid>
</Window>
cs 复制代码
<UserControl x:Class="WpfApp10.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApp10"
             mc:Ignorable="d" 
             d:DesignHeight="200" d:DesignWidth="200" Background="AliceBlue">
    <Grid>
        <StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center" Height="30">
            <TextBox Width="100" x:Name="tb1" x:FieldModifier="private"></TextBox>
        </StackPanel>
    </Grid>
</UserControl>
cs 复制代码
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;

namespace WpfApp10
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btn1_Click(object sender, RoutedEventArgs e)
        {
            this.uc1.tb1.Text = "Name";
        }
    }

    public class Student
    {
        public string Name { get; set; }
    }
}

周末就跟教程重新敲一遍。

相关推荐
ReadThroughLife15 分钟前
【已解决】MACOS M4 芯片使用 Docker Desktop 工具安装 MICROSOFT SQL SERVER
microsoft·macos·docker·容器
~Yogi2 小时前
今日学习:Spring线程池|并发修改异常|链路丢失|登录续期|VIP过期策略|数值类缓存
学习·spring·缓存
Moonnnn.4 小时前
【单片机期末】单片机系统设计
笔记·单片机·嵌入式硬件·学习
行云流水剑6 小时前
【学习记录】使用 Kali Linux 与 Hashcat 进行 WiFi 安全分析:合法的安全测试指南
linux·学习·安全
门前云梦8 小时前
《C语言·源初法典》---C语言基础(上)
c语言·开发语言·学习
小前端大牛马9 小时前
java教程笔记(十一)-泛型
java·笔记·python
sjtu_cjs9 小时前
Tensorrt python api 10.11.0笔记
开发语言·笔记·python
0x7CF9 小时前
QT3D学习笔记——圆台、圆锥
qt·学习·3d
Blue桃之夭夭9 小时前
深入理解Optional:处理空指针异常
linux·windows·microsoft
qq_3863226910 小时前
华为网路设备学习-24(路由器OSPF - 特性专题)
学习·华为·智能路由器