WPF入门01之从XAML入手1

1.xmal元素介绍
新建一个wpf窗口后,会生成如下的xaml语句

<Window x:Class="ControlExercise.Window1"
        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:ControlExercise"
        mc:Ignorable="d"
        Title="Window1" Height="450" Width="800">
    <Grid>
        
    </Grid>
</Window>

那好,我们就从最简单的,vs框架帮我们生成的代码开始说起。
xaml中最基本的语法元素就是标签、属性、内容。

  • 标签:

通常以<>开始,</>结束,一个标签的声明通常标识一个对象。
两种写法:
1)非自闭合标签<Window></Window>, <Grid></Grid>
2)自闭合标签<Window />, <Grid />

  • 属性:
    通常以键值对形式出现,Title="MainWindow" Height="450" Width="800"

  • 内容:
    一组标签之间夹杂的文本或其他标签,都被称之为这个标签的内容。此处Window标签的内容就是一个<Grid></Grid>标签。

Window x:Class="ControlExercise.Window1" 表示此xaml窗口对应的C#类,为ControlExercise命名空间下的Window1类。

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"表示引用wpf界面表现相关的命名空间,类似于我们C#类中的using。

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"表示引用xaml相关的命名空间。

2.修改wpf的启动窗口
在这里插入图片描述
在App.xaml中的Application标签里,有StartupUri属性,指示了当前程序最先启动的窗口,如果我们的程序有很多窗口,调试的时候比较方便,想启动谁就改成那个窗口

<Application x:Class="ControlExercise.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:ControlExercise"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
         
    </Application.Resources>
</Application>

3.为Grid标签设置背景颜色
元素的属性也可以通过标签来表示,也可以在Grid标签直接写BackGround,两种形式都表示设置Grid的Background属性值。

<Grid>
        <Grid.Background>
            <SolidColorBrush Color="Blue">
                
            </SolidColorBrush>
        </Grid.Background>
    </Grid>
<Grid Background="Blue">
        
    </Grid>

xaml中所有元素都是由属性,标签,内容构成的,只有先了解了这些基本概念后,才能方便以后的继续学习。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苏克贝塔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值