XAML基本语法与例子

XAML (eXtensible Application Markup Language) 是一种基于 XML 的声明性语言,主要用于 WPF、UWP、Xamarin.Forms 和 MAUI 等框架中构建用户界面。

基本语法结构

1. 根元素和命名空间声明

<Page x:Class="MyNamespace.MyPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="clr-namespace:MyNamespace">
    <!-- 内容 -->
</Page>

2. 对象元素语法

<Button Content="点击我" />

3. 属性语法

<Button Content="点击我" Background="LightBlue" Width="100" Height="30" />

4. 属性元素语法

<Button Width="100" Height="30">
    <Button.Content>
        <StackPanel Orientation="Horizontal">
            <Image Source="icon.png" Width="16" Height="16"/>
            <TextBlock Text="点击我" Margin="5,0,0,0"/>
        </StackPanel>
    </Button.Content>
</Button>

5. 集合语法

<StackPanel>
    <StackPanel.Children>
        <Button Content="按钮1"/>
        <Button Content="按钮2"/>
        <Button Content="按钮3"/>
    </StackPanel.Children>
</StackPanel>

<!-- 简写形式 -->
<StackPanel>
    <Button Content="按钮1"/>
    <Button Content="按钮2"/>
    <Button Content="按钮3"/>
</StackPanel>

6. 内容属性语法

<!-- Label 的 Content 是内容属性 -->
<Label>这是一个标签</Label>

<!-- 等同于 -->
<Label Content="这是一个标签"/>

7. 标记扩展

<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值