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

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

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

  • 17
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值