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

App.xml和Application类简介

概述

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

App.xaml

csharp 复制代码
<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

csharp 复制代码
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

csharp 复制代码
<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

csharp 复制代码
<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生命周期

csharp 复制代码
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);
        }

    }

}

窗体的声明周期

csharp 复制代码
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");
        }


    }
}
相关推荐
JustCheng21 小时前
Dispacher(1/2)深入解析-详细使用
c#
Interview Aid1121 天前
TikTok OA 四题分享|半小时内 AC,题目基本都是实现题
java·开发语言·算法·面试·职场和发展
CoderIsArt1 天前
C#中UI 线程与 Dispatcher
开发语言·ui·c#
AI情绪识别开源2 天前
检信 ALLEMOTION OS 加密打包可执行程序 — 全面测试报告版本: v1.3功能测试 / 性能测试 /
开发语言·数据结构·人工智能·功能测试
「QT(C++)开发工程师」2 天前
C++ auto 用法详解
开发语言·c++
OPEN-F2 天前
C++STL教程:容器适配器与实用工具
开发语言·c++
yaoxin5211232 天前
507. Java 反射 - 在 BeanFactory 中实现依赖注入
java·开发语言
OPEN-F2 天前
C++模板教程:变参模板、折叠表达式与SFINAE
java·开发语言·c++
有点。2 天前
C++二叉搜索树进阶
开发语言·c++