《深入浅出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; }
    }
}

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

相关推荐
小白黑_2165 分钟前
设计模式笔记
笔记·设计模式
杰尼杰尼丶25 分钟前
MQTTnet.Extensions.ManagedClient客户端连接
mqtt·unity·c#
新手unity自用笔记26 分钟前
项目-坦克大战学习笔记-按键按下控制方向
笔记·学习·c#
调了个寂寞37 分钟前
INS风格时尚自拍人像摄影后期Lr调色,手机滤镜PS+Lightroom预设下载!
笔记
瞌睡不来39 分钟前
(刷题记录5)盛最多水的容器
c++·笔记·学习·题目记录
一 乐1 小时前
考研论坛平台|考研论坛小程序系统|基于java和微信小程序的考研论坛平台小程序设计与实现(源码+数据库+文档)
java·数据库·学习·考研·微信·小程序·源码
William_Edmund2 小时前
C++ 算法学习——1.8 悬线法
学习
朝九晚五ฺ2 小时前
【Linux探索学习】第三弹——Linux的基础指令(下)——开启新篇章的大门
linux·运维·学习
君莫愁。2 小时前
【Unity】双摄像机叠加渲染
unity·c#·游戏引擎
Vae_Mars2 小时前
WPF中的switch选择
开发语言·c#