【上位机——WPF】App.xml和Application类简介

发布于:2025-05-16 ⋅ 阅读:(13) ⋅ 点赞:(0)

概述

xaml类型的文件包含两部分,一部分以.xaml扩展名结尾的前端代码,另一部分以.xaml.cs结尾的后端代码,通常我们也把后端代码称为隐藏代码。

App.xaml

<Application x:Class="WpfDemo1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfDemo1"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
         <!--这里编写一些资源 通用样式-->
    </Application.Resources>
</Application>

x:Class=“WpfDemo1.App” :指的是App.xaml.cs文件中的App类
xmlns:local=“clr-namespace:WpfDemo1” :指的是本地的命名空间(也是项目名)
StartupUri=“MainWindow.xaml”:指定程序要启动的窗口

App.xaml.cs


using System.Configuration;
using System.Data;
using System.Windows;

namespace WpfDemo1
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
    }

}


Application:继承了所有控件的祖先

入门代码

App.xaml

<Application x:Class="WpfDemo1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfDemo1"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <!--定义Button按钮前景色为红色-->
            <Style TargetType="Button" x:Key="ButtonStyle">
                <Setter Property="Foreground" Value="Red"></Setter>
            </Style>
        </ResourceDictionary>
    </Application.Resources>
</Application>

MainWindow.xml

<Window x:Class="WpfDemo1.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:WpfDemo1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <!--Style 加载的是App.xaml文件中的Style标签 key为ButtonStyle的样式-->
        <Button Content="Hello world" Width="200" Height="40"  Style="{StaticResource ButtonStyle}"></Button>
    </Grid>
</Window>

Application生命周期

using System.Configuration;
using System.Data;
using System.Windows;

namespace WpfDemo1
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {

        /// <summary>
        /// 应用程序启动时,做一些初始化的工作
        /// </summary>
        /// <param name="e"></param>
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
        }

        /// <summary>
        /// 当应用程序结束时
        /// </summary>
        /// <param name="e"></param>
        protected override void OnExit(ExitEventArgs e)
        {
            base.OnExit(e);
        }

        /// <summary>
        /// 当应用程序被激活时
        /// </summary>
        /// <param name="e"></param>
        protected override void OnActivated(EventArgs e)
        {
            base.OnActivated(e);
        }

        /// <summary>
        /// 当应用程序处于非激活状态
        /// </summary>
        /// <param name="e"></param>
        protected override void OnDeactivated(EventArgs e)
        {
            base.OnDeactivated(e);
        }

    }

}

窗体的声明周期

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 WpfDemo1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            //创建窗体源时引发此事件 比load还要提前
            this.SourceInitialized += (o, e) => {
                Console.WriteLine("SourceInitialized");
            };
            //窗体加载时引发此事件
            this.Loaded += (o, e) => {
                Console.WriteLine("Loaded");
            };

            //窗体被激活引发此事件
            this.Activated += (o, e) => {
                Console.WriteLine("Activated");
            };

            //窗体非激活引发此事件
            this.Deactivated += (o, e) => {
                Console.WriteLine("Deactivated");
            };

            //释放资源引发此事件
            this.Unloaded += (o, e) => { Console.WriteLine("Unloaded"); };

            this.Closing += (o, e) => { Console.WriteLine("Closing"); };

            this.Closed += (o, e) => { Console.WriteLine("Closed"); };

            //渲染页面
            this.ContentRendered += (o, e) => { Console.WriteLine("ContentRendered"); };
            //等同于
            //this.ContentRendered += OnContentRendered;

        }

        private void OnContentRendered(object sender,EventArgs e) {
            Console.WriteLine("ContentRendered");
        }


    }
}

网站公告

今日签到

点亮在社区的每一天
去签到