135-1821-9792

WPF应用程序管理(二)

一、WPF 应用程序由 System.Windows.Application 类进行管理

目前创新互联已为千余家的企业提供了网站建设、域名、网页空间、网站托管、企业网站设计、崇义网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

二、创建 WPF 应用程序

创建 WPF 应用程序有两种方式:
1、Visual Studio 和 Expression Blend 默认的方式,使用 App.xaml 文件定义启动应用
程序
App.xaml 文件的内容大致如下:

1: 2: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presen
tation"
3: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4: StartupUri="Window1.xaml">
5:
6:

7:

其中 StartupUri 指定启动的 WPF 窗体
2、可以自已定义类,定义 Main 方法实现对 WPF 应用程序的启动
在项目中添加一个类,类的代码如下,在项目选项中,设定此类为启动项。
1: using System;
2: using System.Windows;
3:
4: namespace WpfApplicationLifeCycle
5: {
6: public class MainClass
7: {
8: [STAThread]
9: static void Main()
10: {
11: // 定义 Application 对象
12: Application app = new Application();
13:
14: // 方法一:调用 Run 方法,参数为启动的窗体对象
15: Window2 win = new Window2();
16: app.Run(win);
17:
18: // 方法二:指定 Application 对象的 MainWindow 属性为启动窗体,
调用无参数的 Run 方法
19: //Window2 win = new Window2();
20: //app.MainWindow = win;
21: //win.Show(); // 此处必须有 win.Show(),否则不能
显示窗体
22: //app.Run();
23:
24: // 方法三:
25: //app.StartupUri = new Uri("Window2.xaml", UriKind.R
elative);
26: //app.Run();
27: }
28: }
29: }

三、应用程序关闭
应用程序关闭时的策略由 ShutdownMode 属性指定,其类型为 System.Window
s.ShutdownMode 枚举类型,其枚举成员有:
OnLastWindowClose(默认值):当应用程序中的最后一个窗体关闭时或调用 Applic
ation 对象的 Shutdown()方法时,应用程序关闭;
OnMainWindowClose:当主窗体(即启动窗体)关闭时或调用 Application 对象的 S
hutdown()方法时,应用程序关闭。(类似于 C#的 Windows 应用程序的关闭模式);
OnExplicitShutdown:只有在调用 Application 对象的 Shutdown()方法时,应用
程序才会关闭;
更改的时候,可以直接在 App.xaml 中更改:

1: 2: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presen
tation"
3: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4: StartupUri="Window1.xaml"
5: ShutdownMode="OnExplicitShutdown">
6:
7:

8:

也可以在代码文件(App.xaml.cs)中更改
1: Application app = new Application();
2: Window2 win = new Window2();
3:
4: // 更改关闭模式必须要在调用 app.Run()方法之前
5: app.ShutdownMode = ShutdownMode.OnExplicitShutdown;
6: app.Run(win);

四、Application 对象的事件

WPF 应用程序管理(二)

应用程序的事件处理可以:
1、在 App.xaml 中做事件的绑定,在 App.xaml.cs 文件中添加事件的处理方法
在 App.xaml 文件中:
1: 2: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presen
tation"
3: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4: StartupUri="Window1.xaml"
5: Startup="Application_Startup">
6:
7:

8:

在 App.xaml.cs 文件中:
1: using System.Windows;
2:
3: namespace WpfApplicationLifeCycle
4: {
5: ///


6: /// Interaction logic for App.xaml
7: ///

8: public partial class App : Application
9: {
10: private void Application_Startup(object sender, Startup
EventArgs e)
11: {
12: // 定义应用程序启动时要处理的内容
13: }
14: }
15: }

2、在自定义的类中可以做正常的 C#的事件绑定:
1: [STAThread]
2: static void Main()
3: {
4: // 定义 Application 对象
5: Application app = new Application();
6: Window2 win = new Window2();
7:
8: // 添加事件的绑定
9: app.Startup += new StartupEventHandler(app_Startup);
10:
11: app.Run(win);
12: }
13:
14: static void app_Startup(object sender, StartupEventArgs e)
15: {
16: Window2 win = new Window2();
win.Show();
win.button1.Content = "YOU!";
17: }

主窗体载入时,会在 Window2 里面定义的 button1 上面就会显示 YOU!
五、WPF 应用程序生存周期

WPF 应用程序管理(二)


当前名称:WPF应用程序管理(二)
文章源于:http://kswsj.com/article/ppeijj.html

其他资讯



Copyright © 2009-2022 www.kswsj.com 成都快上网科技有限公司 版权所有 蜀ICP备19037934号