站长seo工具,徐州网站制作方案,电子书网站模板,网络服务商是指什么C# WPF编程-Application类 应用程序的生命周期创建Application对象应用程序的关闭方式应用程序事件 Application类的任务显示初始界面处理命令行参数访问当前Application对象在窗口之间进行交互 程序集资源添加资源检索资源pack URI内容文件 本地化构建能够本地化的用户界面 每… C# WPF编程-Application类 应用程序的生命周期创建Application对象应用程序的关闭方式应用程序事件 Application类的任务显示初始界面处理命令行参数访问当前Application对象在窗口之间进行交互 程序集资源添加资源检索资源pack URI内容文件 本地化构建能够本地化的用户界面 每个运行中的WPF应用程序都由System.Windows.Application类的一个实例来表示。该类跟踪在应用程序中打开的所有窗口决定何时关闭应用程序。 应用程序的生命周期
在WPF中应用程序会经历简单的生命周期。本质上Visual Studio为Application类使用的模型与用于窗口的模型相同。起点是XAML模板默认情况下该模板命名为App.xaml
创建Application对象
Application x:ClassWpfApp3.Appxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:localclr-namespace:WpfApp3StartupUriMainWindow.xamlApplication.Resources/Application.Resources
/Application
StartupUri属性来确定主窗口的XAML文档。因此不需要代码显式地实例化窗口XAML解析器自动完成这项工作。自动生成的部分在项目中是不可见的看起来如下
using System;
using System.Windows;
public partial class App:Application
{[STAThread()]public static void Main(){WpfApp3.App app new WpfApp3.App();app.InitializeComponent();app.Run();}public void InitializeComponent(){this.StartupUri new Uri(Window1.xaml, System.UriKind.Relative);}}应用程序的关闭方式
通常只要有窗口未关闭Application类就保持应用程序处于有效状态。 可通过Appliaction.ShutdownMode属性修改关闭模式枚举值
OnLastWindowClose:默认行为只少有一个窗口存在应用程序就保持运行状态。OnMainWindowClose:传统方式只要主窗口还处于打开状态应用程序就保持运行状态。OnExplictitShutdown:除非调用Application.Shutdown()方法否则应用程序不会结束。
App.xaml文件中添加ShutdownMode“OnMainWindowClose”
Application x:ClassWpfApp3.Appxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:localclr-namespace:WpfApp3StartupUriMainWindow.xamlShutdownModeOnMainWindowCloseApplication.Resources/Application.Resources
/Application
应用程序事件
App.xaml.cs文件里可添加代码来处理应用程序事件。 应用程序事件
Startup:该事件在调用Application.Run()方法之后并且在主窗口显示之前。Exit:该事件在应用程序关闭时并在Run()方法即将返回之前发生。SessionEnding:该事件在Window对话结束时发生。Activated:当激活应用程序中的窗口是发生该事件。Deactivated:当取消激活用用程序中的窗口时发生该事件。DispatcherUnhandledException:在应用程序中的任何位置只要发送未处理的异常就会发生该事件。
处理事件两种方法
关联事件处理程序重写相应的受保护方法。
关联事件处理程序如xmal中添加事件处理DispatcherUnhandledException“Application_DispatcherUnhandledException” App.xaml
Application x:ClassWpfApp3.Appxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:localclr-namespace:WpfApp3StartupUriMainWindow.xamlDispatcherUnhandledExceptionApplication_DispatcherUnhandledExceptionApplication.Resources/Application.Resources
/Application
App.xaml.cs:
private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{}代码重写事件方法窗口事件
using System.Configuration;
using System.Data;
using System.Windows;namespace WpfApp3
{/// summary/// Interaction logic for App.xaml/// /summarypublic partial class App : Application{private bool unsaveDate false;public bool UnsaveDate {get { return unsaveDate; }set { unsaveDate value; }}protected override void OnStartup(StartupEventArgs e){base.OnStartup(e);UnsaveDate true;}protected override void OnSessionEnding(SessionEndingCancelEventArgs e){base.OnSessionEnding(e);if (UnsaveDate){ e.Cancel true;MessageBox.Show(测试e.ReasonSessionEnding.ToString());}} }
}
Application类的任务
显示初始界面
WPF应用程序的运行速度快但并不能在瞬间启动。第一次启动应用程序时会有一些延迟因为公共语言运行时Common Language Runtime,CLR首先需要初始化.NET环境然后启动应用程序。通常这一延时时间很短。但如果具有更耗时的初始化步骤可使用WPF提供的简单初始界面特性添加加初始界面的方法:
为项目添加图像文件.bmp,.png,.jpg文件。在Solution Explorer中选择图像文件。将Build Action修改为SplashScreen。 下次运行应用程序时图像会立即在屏幕中央显示出来。当添加初始界面时WPF编译器为自动生成的App.cs文件添加与下面类似的代码
SplashScreen splashScreen new SplashScreen(splashScreenImage.png);
splashScreen.show(true);
MyApplication.App app new MyApplication.App();
app.InitializeComponent();
app.Run();处理命令行参数
为处理命令行参数需要响应Application.Startup事件。命令行参数是通过StartupEventArgs.Args属性作为字符串数组提供的。 例如假定希望加载文档文档名作为命令行参数传递。通过代码实例化主窗口。
public partial class App : Application
{private static void App_Startup(object sender, StartupEventArgs e){FileViewer win new FileViewer();if (e.Args.Length 0){string file e.Args[0];if (System.IO.File.Exists(file)){win.LoadFile(file);}else{}}}
}访问当前Application对象
通过静态的Application.Current属性可在应用程序的任何位置获取当前应用程序的实例从而在窗口之间进行基本交互任何窗口都可以访问当前Application对象并通过Application对象获取主窗口的引用 Window main Application.Current.MainWindow; MessageBox.Show(The main window is main.Title);
如果希望访问在自定义窗口类中添加的任意方法、属性或事件需要将窗口对象转装换为正确类型。 MainWindow main (MainWindow)Application.Current.MainWindow; main.DoSomething();
在窗口中还可以检查Application.Windows集合的内容 foreach( Window window in Application.Current.Windows) { MessageBox.Show(window.Title is open.); }
在窗口之间进行交互
应用程序类还可以很好地达到另一个目的保存重要窗口的引用使一个窗口可访问另一个窗口。
窗口分为模态和非模态
模态窗口模态窗口会中断应用程序流直到窗口关闭为止。非模态窗口非模态窗口则不中断应用程序流。
示例每个文档窗口由名为Document的类实例表示
public partial class App : Application
{private ListDocument documents new ListDocument();public ListDocument Documents{get {return documents};set {documents value;}}
}下面是响应按钮点击事件的处理程序
private void cmdCreate_Click(object sender, RouteEventArgs e)
{Document doc new Document();doc.Owner this;doc.Show();((App)Application.Current).Documents.Add(doc);
}程序集资源
WPF应用程序中的程序集资源与其他.NET应用程序中的程序集资源在本质上是相同的。基本概念是为项目添加文件从二Visual Studio可将其嵌入到编译过的应用程序的EXE或DLL文件中。
添加资源
通过向项目添加文件并在Properties窗口中将其Build Action属性设置为Resource来添加资源。 为成功地使用程序集资源未必注意一下两点
不能将Build Action属性错误地设置为Embedded Resource。不要在Project Properties窗口中使用Resource选项卡。
检索资源
可以采用多种方法来使用资源。 低级方法是检索封装数据的StreamResourceInfo对象然后决定如何使用该对象。 StreamResourceInfo sri Application.GetResourceStream(new Uri(“images/winter.jpg”, UriKind.Relative));
XMAL: \Image Source“Images/1.jpg”/Image
使用BitmapImage对象该对象使用URI确定希望显示的图像位置。 绝对路径 img.Source new BitmapImage(new Uri((“d:\Img\jpgs\2.jpg”));
相对路径 img.Source new BitmapImage(new Uri(“images/6.jpg”, UriKind.Relative));
pack URI
WPF使用pack URI语法寻址编译个的资源。使用相对URI来引用资源 images/2.jpg
内容文件
当嵌入式文件作为资源时会将文件放到编译过的程序集中并且可以确保文件总是可用的。 如下情况不适合使用这种方法
希望改变资源文件有不想重新编译应用程序资源文件非常大资源文件是可选的并且可以不随程序集一起部署资源是声音文件WPF声音类不支持程序集资源
WPF为程序集添加了AssemblyAssociatedContentFile特性声明每个内容文件的存在。 为项目添加音频文件
在Solution Explorer中选择该文件并在Properties中将Build Action属性改为Content。 \MediaElement Name“Sound” Source“Sounds/1.wav” LoadeBehavior“Manual”/MediaElement
本地化
构建能够本地化的用户界面