手动创建应用程序
1.创建Empty Project
2.添加引用
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
通过向导创建应用程序
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 = 0, // // Summary: // An application shuts down when either the main window closes, or System.Windows.Application.Shutdown() // is called. OnMainWindowClose = 1, // // Summary: // An application shuts down only when System.Windows.Application.Shutdown() // is called. OnExplicitShutdown = 2, }
Shutdown函数有两种形式,不传任何参数、传入Int作为ExitCode
To be continue...