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

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

相关推荐
小葡萄202536 分钟前
黑马程序员C++2024新版笔记 第三章 数组
笔记·算法·c++20
编程乐趣1 小时前
5个yyds的.Net商城开源项目
开源·c#·.net
虾球xz2 小时前
游戏引擎学习第292天:实现蛇
c++·学习·游戏引擎
DanmF--2 小时前
Protobuf协议生成和使用
网络·unity·c#·游戏引擎·游戏程序
wishfly3 小时前
vscode - 笔记
ide·笔记·vscode
黄鹂绿柳4 小时前
Vue+Vite学习笔记
vue.js·笔记·学习
985小水博一枚呀8 小时前
【AI大模型学习路线】第二阶段之RAG基础与架构——第七章(【项目实战】基于RAG的PDF文档助手)技术方案与架构设计?
人工智能·学习·语言模型·架构·大模型
她说彩礼65万8 小时前
C# lock
c#
路人与大师8 小时前
从lightrag的prompt到基于openai Structured Outputs 的优化实现思路
windows·microsoft·prompt
FakeOccupational8 小时前
计算机科技笔记: 容错计算机设计05 n模冗余系统 TMR 三模冗余系统
笔记·科技