XAML

定义程序界面的标记语言

XAML(Extensible Application Markup Language 聆听i/ˈzæməl/)是Windows Presentation Foundation(WPF)和Universal Windows Platform(UWP)的一部分,是微软开发的一种基于XML、基于声明,用于初始化结构化值和对象的用户界面宣告式程序设计,它有着HTML的外观,又揉合了XML语法的本质,例如:可以使用<Button>标签设置按钮(Button)。它类似Linux平台下的glade。至于WinFX XAML Browser Application(XBAP)是用XAML作界面描述,在浏览器中执行的程序,可取代过去的ActiveXJava AppletFlash

XAML本质上属于一种.NET编程语言,属于通用语言运行库(Common Language Runtime),同C#VB.NET等同。与HTML类似,特点是用来描述用户界面。XAML的语法格式为:<Application... />,Application是必备的基本元素。XAML可以定义2D和3D对象、旋转(rotations)、动画(animations),以及各式各样的效果。

Hello world 编辑

  • C# UWP
<Page
    x:Class="UwpAppExample.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:UwpAppExample"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid>
        <Button Margin="10,0,10,0" Height="100" Click="Hello_Click" Content="Click Me!"/>
    </Grid>

</Page>


using System;
using Windows.UI.Xaml.Controls;
namespace UwpAppExample
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }
        private async void Button_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            await new Windows.UI.Popups.MessageDialog("Hello World!").ShowAsync();
        }
    }
}
  • C# WPF
<Window x:Class="WpfAppExample.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:WpfAppExample"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Margin="10,0,10,0" Height="100" Click="Hello_Click" Content="Click Me!"/>
    </Grid>
</Window>


using System.Windows;

namespace WpfAppExample
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Hello_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Hello World!");
        }
    }
}
  • Visual Basic dotNet
<Window x:Class="WpfAppExample.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:WpfAppExample"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Margin="10,0,10,0" Height="100" Click="Hello_Click" Content="Click Me!"/>
    </Grid>
</Window>


Imports System.Windows

Class MainWindow
    Sub New()
        InitializeComponent()
    End Sub

    Private Sub Hello_Click(sender As Object, e As RoutedEventArgs)
        MessageBox.Show("Hello World!")
    End Sub
End Class

参考资料 编辑

外部链接 编辑