首页 > 其他分享 >《深入浅出WPF》学习笔记三.x命名空间以及常见属性

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

时间:2024-08-01 23:25:49浏览次数:12  
标签:Name Windows 深入浅出 System 笔记 public using WPF MainWindow

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

x:ClassModifier="public"

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

    public partial class MainWindow : Window

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

x:Name

作用:

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

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

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

x:Name和Name的区别

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

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

<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>
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   

<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>
<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>
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; }
    }
}

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

标签:Name,Windows,深入浅出,System,笔记,public,using,WPF,MainWindow
From: https://blog.csdn.net/yannsann/article/details/140859162

相关文章

  • wpf基础
    在WPF(WindowsPresentationFoundation)中,Style是一种强大的资源,允许你定义一组属性值,这些值可以被多个控件实例共享。使用Style可以减少重复的XAML代码,并且使得UI的一致性和可维护性得到提高。以下是一些Style的基本概念和用法:定义Style你可以在XAML中的......
  • 机器学习笔记 - RAFT 光流简读
    一、光流        光流是图像序列中像素的表观运动。为了估计光流,场景中物体的移动必须具有相应的亮度位移。这意味着一个图像中移动的红球在下一个图像中应该具有相同的亮度和颜色,这使我们能够确定它以像素为单位移动了多少。下图显示了光流示例,其中一系列图像捕获了......
  • Min-Max 容斥学习笔记
    \(\text{Min-Max}\)容斥学习笔记概念\(\text{Min-Max}\)容斥,又称最值反演,是一种对于特定集合,在已知最小值或最大值中一者的情况下,求另一种的算法。首先观察几个式子:\[\max(a)=a\\\max(a,b)=a+b-\min(a,b)\\\max(a,b,c)=a+b+c-\min(a,b)-\min(b,c)-\min(a,c)+\min(a,b,c)\]......
  • 最全个人笔记【Makefile工程管理】
    1.基本概念1.1make是什么当一个项目中要编译的文件很多时,手工使用编译器一个个进行编译,很明显不具有可操作性,此时必须借助某些软件,协助我们有序地、正确地自动编译整个工程的所有该编译的文件。这样的软件被称为工程管理器,make就是一款工程管理器软件。1.2Makefile......
  • Linux基础笔记
    快捷键的使用1、终端操作打开终端(图像化界面)1.鼠标右击+E键(先后按键)2.ctrl键+shift键+t键打开多个终端2、什么是Linux终端?Linux终端又称为什么?Linux终端也称为虚拟控制台,是Linux从UNIX继承来的标准特性。显示器和键盘合称为终端,因为它们可以对系统进行控制,所以又......
  • PCIe学习笔记(11)
    TPH规则•TPH指定了两种格式。所有提供TPH的请求都必须使用Baseline(基线)TPH格式。带有可选TPHTLP前缀的格式扩展了TPH字段,为SteetingTag(转向标签,ST)字段提供了额外的位,此时,TLPheaderByte0-3如下图。•可选的TPHTLPPrefix用于扩展TPH字段。◦TPHTLP前缀的存在是......
  • PCIe学习笔记(14)
    Vendor_Defined消息Vendor_DefinedMessages允许扩展PCIExpress消息传递功能,既可以作为PCIExpress规范的一般扩展,也可以作为特定于供应商的扩展。此处定义与这些消息关联的规则。MessageCode数量有限,PCIE协议定义了VDM(VendorDefinedMessage),以此来扩展Message种类。......
  • Living-Dream 系列笔记 第71期
    众所周知,换根dp是非常套路的。换根真好玩(换根dp:当不同节点作为根时,dp结果不一致,若枚举每个节点作为根,则时间复杂度过高,在此种情形下,可使用换根dp处理相邻两节点间的贡献,从而达到快速换根的效果。使用场景:对于一棵树,寻找以某节点\(u\)为根时取得的最大值/最小值......
  • 旧笔记本安装Win8.1实录
    昨天发现一台尘封已久的LenovoideapadY550,给它装上了Windows10然后第二天系统挂掉了挂的原因是半夜万恶之源Windows更新开始造孽了刚好没电文件全坏了真解除封印因为文件已经没了我索性直接重装系统,降级到Win8.1真香!系统是Win8.1withupdate的精简版,开始菜单有关......
  • [学习笔记] 斜率优化
    引入斜率优化用于求解类似于\(f_i=f_j+a_ib_j+c_i\)使\(f_i\)最大或最小之类的形式的DP转移,标志就是其中有一项(如\(a_ib_j\))与\(i,j\)均有关联。求解令\(j\)为\(i\)的最优决策点,也就是\(f_i=f_j+a_ib_j+c_i\),我们将其进行一些移项可以得到\(f_j=-......