首页 技术 正文
技术 2022年11月18日
0 收藏 829 点赞 4,495 浏览 3044 个字

手动创建应用程序

1.创建Empty Project

WPF学习之路(七)应用程序和窗口

2.添加引用

WPF学习之路(七)应用程序和窗口

3.添加 ManualApp.cs 并添加下面的代码

[STAThread]
public static void Main()
{
Window win = new Window();
win.Title = "Manually created application";
win.Show();
Application app = new Application();
app.Run();
}
[STAThread] 单线程模型

4.执行该程序会多出现一个控制台窗口,可以修改Project的Output Type

WPF学习之路(七)应用程序和窗口

 通过向导创建应用程序

Main函数实现在  \obj\Debug\App.g.cs中

应用程序的生命周期

Application开始Run以后,进入消息循环,不断响应并处理事件

应用程序入口

应用程序入口都是Main函数,两种主要类型的对象Application\Window,前者在全局中是唯一的可以通过Application.Current获取。

[System.STAThreadAttribute()]
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
public static void Main() {
Alex_WPFAPPDemo04.App app = new Alex_WPFAPPDemo04.App();
app.InitializeComponent();
app.Run();
}

应用程序起点

第一个响应的事件是Startup

App.xaml中默认使用下面的方式

<Application x:Class="Alex_WPFAPPDemo04.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="Application_StartUp">

稍作修改,会最先进入到Application_StartUp事件处理方法

<Application x:Class="Alex_WPFAPPDemo04.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="Application_StartUp">

应用程序的状态

应用程序有两种状态,Actived/Deactived,WPF可以通过Actived/Deactived事件来处理这两种状态

<Application x:Class="Alex_WPFAPPDemo04.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml"
Activated="Application_Actived"
Deactivated="Application_Deactived">

应用程序的异常

<Application x:Class="Alex_WPFAPPDemo04.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml"
DispatcherUnhandledException="Application_DispatcherUnhandledException">

当应用程序发生异常时,会触发DispatcherUnhandledException事件,实现该事件处理逻辑可以防止程序崩溃

非正常退出

当操作系统关机、重启、注销等操作时,应用程序会接受SessionEnding事件,通过设置Cancel属性可以组织操作系统的关机、重启、注销等操作

<Application x:Class="Alex_WPFAPPDemo04.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml"
SessionEnding="Application_SessionEnding">
private void Application_SessionEnding(object sender, SessionEndingCancelEventArgs e)
{
string msg = string.Format("{0}. End Session?", e.ReasonSessionEnding);
MessageBoxResult result = MessageBox.Show(msg, "Session Ending", MessageBoxButton.YesNo);
if (result == MessageBoxResult.No)
{
e.Cancel = true;
}
}

正常退出

当关闭应用程序主窗口、所有窗口、显示调用Shutdown函数时,程序会正常退出

这3种情况由ShutdownMode属性决定

  // Summary:
// Specifies how an application will shutdown. Used by the System.Windows.Application.ShutdownMode
// property.
public enum ShutdownMode
{
// Summary:
// An application shuts down when either the last window closes, or System.Windows.Application.Shutdown()
// is called.
OnLastWindowClose = ,
//
// Summary:
// An application shuts down when either the main window closes, or System.Windows.Application.Shutdown()
// is called.
OnMainWindowClose = ,
//
// Summary:
// An application shuts down only when System.Windows.Application.Shutdown()
// is called.
OnExplicitShutdown = ,
}

Shutdown函数有两种形式,不传任何参数、传入Int作为ExitCode

To be continue…

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,580
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918