文章

WPF基础——开始

开始

WPF是Windows平台的UI框架,使用C#和xaml语言编写,xaml语言是xml语言的扩展

使用Visual Studio创建WPF应用程序,生成如下文件

image-20240311194923473

  • 依赖项中NETCore.App是.NET平台应用程序的依赖,WindowDeskTop.App.WPF是WPF框架的依赖

  • App.xaml:描述整个应用程序

    • StartupUri:指明主页面
    • xmlns:local:将项目代码的命名空间引入到xaml中
    • Application.Resources:声明当前应用中使用的资源(自定义类等)
    1
    2
    3
    4
    5
    6
    7
    8
    
    <Application x:Class="frontend.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:local="clr-namespace:frontend"
                 StartupUri="MainWindow.xaml">
        <Application.Resources> 
        </Application.Resources>
    </Application>
    
  • MainWindow.xaml:描述主页面布局

    • x:Class:指明该布局编译生成的类

      一个界面后台类通常使用partial关键字声明,表示将该类的定义拆分,如后台C#类是MainWindow,使用partial声明,xaml中x:Class="MainWindow",表示该xaml编译生成一个界面类,在编译时会合并到MainWindow类中

    • xmlns:local:将项目代码的命名空间引入到xaml中

    • Window.Resources:声明当前窗口中的资源

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    <Window x:Class="frontend.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:frontend"
            mc:Ignorable="d"
            Title="MainWindow" Height="450" Width="800">
        <Window.Resources>
        </Window.Resources>
        <Grid>
        </Grid>
    </Window>
    
本文由作者按照 CC BY 4.0 进行授权