一、概述
WPF (Windows Presentation Foundation) 是在微软推出的基于 Window Vista 的用户界面框架,属于 .NET Framework 3.0 的一部分。它提供了统一的变编程模型、语言和框架,真正做到了了分离界面设计人员与开发人员的工作,同时它提供了全新的多媒体交互用户图形界面
二、XAML
在WPF中提供了两种API,可以供我们绘制界面、编写程序逻辑。
- 第一种API是用于普通编程的API,比如用C#、VB.NET等语言进行编程
- 第二种API是基于XML的API,称为XAML(Extensible Application Markup Language),引入XAML使得UI代码和应用程序逻辑代码完全分离,它是一种标记语言,支持声明式编程,由于XAML是基于XML的,所以它拥有XAML的所有规则和定义,非常容易进行扩展。
接下对这两种进行演示:
基于C#
在MainWindow.xaml.cs文件中
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 WPFDemo
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Loaded += MainWindow_Loaded;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
// 创建一个Button
Button myButton = new Button();
myButton.Content = "Click Me!";
myButton.Width = 100;
myButton.Height = 30;
myButton.Click += MyButton_Click;
// 将Button添加到窗口中
this.Content = myButton;
}
// 按钮点击事件
private void MyButton_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Button clicked!");
}
}
}
基于XAML
MainWindow.xaml(UI代码)
<!--WPFDemo就是解决方案名称-->
<Window x:Class="WPFDemo.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:WPFDemo"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Button Width="100" Height="30" Click="MyButton_Click">Click me</Button>
</Window>
MainWindow.xaml.cs(应用程序逻辑代码)
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 WPFDemo
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
// 按钮点击事件
private void MyButton_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("this is xaml");
}
}
}
XAML 中使用最多的XML功能应该有三个: 命名空间、属性和子元素。
可以看一个简单案例:
<Window x:Class="WPFDemo.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:WPFDemo"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Button Width="100" Height="30" Click="MyButton_Click">
<Button.Background>white</Button.Background>
<Button.Content>按钮</Button.Content>
</Button>
</Window>
- 命名空间: xmlns就是XML中的命名空间,简单来说就是xmlns提供了一种方法把URI引用的命名空间定义为当前XML文件的元素和属性的默认命名空间。这里表示当前这个XML文档,也就是我们的XMAL文件,他的默认的命名空间就是:schemas.microsoft.com/winfx/2006/…
- 属性: 属性的表达方式有两种。
第一种就是直接用属性表示,比如Width。
第二种是使用子元素的形式,比如Background。