wpf之一:基础概念

来源:互联网 发布:android 网络编程 pdf 编辑:程序博客网 时间:2024/06/16 05:28

1.wpf程序入口main函数

我们新建一个wpf的程序,vs自动给我们生成了一个MainWindow.xaml和App.xaml文件。

微软官方说wpf程序是从Application开始的,既然是开始总有个入口点吧,奇怪的是我们并没有发现Main函数,程序又是如何Run起来的呢?

其实,wpf为了简化我们的工作,把一些机械性的代码透明了,那么我们如何找到这个Main函数呢?很简单,我们编译一下程序,发现

App.xaml最后生成了App.g.cs的部分类,并且发现StartupUri是MainWindow.xaml,也就是说程序一运行,MainWindow.xaml将会启动。




2.Wpf中Application的生命周期

    我们知道webform中的Global文件定义了一个应用程序的全局生命周期,或许有人问,生命周期能够干些什么,其实干的事情可多着呢,

比如我们可以做一些身份验证,或者一些信息的初始化,那么wpf中到底有哪些对应的方法和事件呢?

1:OnStartup方法    =>   Startup 事件

     这个就见名识意了,也就是上面一幅图中的app.Run()的时候触发。

2: OnSessionEnding方法 => SessionEnding 事件

     系统关机前调用。

3:OnExit方法 => Exit事件

     应用程序关闭前调用。

4:OnActivated方法 =>  Activated 事件

     应用程序获得焦点的时候触发。

5:OnDeactivated方法 => DeActivated事件

     应用程序失去焦点的时候触发。

using System;using System.Collections.Generic;using System.Configuration;using System.Data;using System.Linq;using System.Threading.Tasks;using System.Windows;namespace Test{    /// <summary>    /// App.xaml 的交互逻辑    /// </summary>    public partial class App : Application    {        protected override void OnActivated(EventArgs e)        {            base.OnActivated(e);            //TODO  your code        }        protected override void OnDeactivated(EventArgs e)        {            base.OnDeactivated(e);            //TODO  your code        }        protected override void OnExit(ExitEventArgs e)        {            base.OnExit(e);            //TODO  your code        }        protected override void OnStartup(StartupEventArgs e)        {            base.OnStartup(e);            //TODO  your code        }        protected override void OnSessionEnding(SessionEndingCancelEventArgs e)        {            base.OnSessionEnding(e);            //TODO  your code        }    }}

3.全局异常获取

     在webform中的Global文件中有一个Application_Error方法,专门用来捕获整个应用程序的异常,以至于不会出现“黄白页”给用户,以此来提高

系统的健壮性和安全性,那么wpf中也有类似的方法吗?当然,wpf跟webform神似,他有的我也有,这里是一个DispatcherUnhandledException

事件,然后我们在OnStartup注册一下就Ok了。

using System;using System.Collections.Generic;using System.Configuration;using System.Data;using System.Linq;using System.Threading.Tasks;using System.Windows;using System.Windows.Threading;namespace Test{    /// <summary>    /// App.xaml 的交互逻辑    /// </summary>    public partial class App : Application    {        protected override void OnStartup(StartupEventArgs e)         {             base.OnStartup(e);              //注册Application_Error             this.DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);          }        //异常处理逻辑        private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)        {            MessageBox.Show("谁tmd惹祸了:" + e.Exception.Message);            //处理完后,我们需要将Handler=true表示已此异常已处理过            e.Handled = true;        }        protected override void OnActivated(EventArgs e)        {            base.OnActivated(e);            //TODO  your code        }        protected override void OnDeactivated(EventArgs e)        {            base.OnDeactivated(e);            //TODO  your code        }        protected override void OnExit(ExitEventArgs e)        {            base.OnExit(e);            //TODO  your code        }        protected override void OnSessionEnding(SessionEndingCancelEventArgs e)        {            base.OnSessionEnding(e);            //TODO  your code        }    }}

好,下面我们做了示例:

首先我们拖一个button,事件处理中故意抛出异常。

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;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 Test{    /// <summary>    /// MainWindow.xaml 的交互逻辑    /// </summary>    public partial class MainWindow : Window    {        public MainWindow()        {            InitializeComponent();        }        private void button_Click(object sender, RoutedEventArgs e)        {            throw new Exception("我就害你,我就抛异常");        }    }}
最后看一下效果,



1 0