-
WPF主要使用一下库:
-
PresentationCore.dll 核心库
-
PresentationFramework.dll 控件库
-
System.Xaml.dll Xaml 解析库
-
WindowBase 窗口基类库
-
App类继承Application类几个重要的方法:
//应用激活获取到焦点触发
protected override void OnActivated(EventArgs e)
{ base.OnActivated(e); }
//应用激活获取到焦点触发
protected override void OnDeactivated(EventArgs e)
{ base.OnDeactivated(e); }
//应用关闭时触发
protected override void OnExit(ExitEventArgs e)
{ base.OnExit(e); }
//应用程序启动时触发
protected override void OnStartup(StartupEventArgs e)
{ base.OnStartup(e); }
//系统关机时触发
protected override void OnSessionEnding(SessionEndingCancelEventArgs e)
{ base.OnSessionEnding(e); }常用属性: Application.MainWindow 应用的主窗体,不修改的话,默认为第一次show的窗体 Application.Windows 应用包含的所有实例窗体的集合 Application.ShouDownModel 应用关闭,生命周期结束的模式。三种枚举方式 Application.Current 当前应用的属性
-
Window窗口的常用属性
Owner---->当前窗口属于那个窗口,会显示在该窗口前面
Owners—>当前窗口是哪些窗口的Owner -
WPF全局异常的调用:
this.DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);常用方法是将此放到OnStartup方法中调用。
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
//注册Application_Error
this.DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);
}
//异常处理逻辑
void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
//处理完后,我们需要将Handler=true表示已此异常已处理过
e.Handled = true;
}
}例子. private void button1_Click(object sender, RoutedEventArgs e)
{
throw new Exception(“我要抛异常”);
}
//异常处理逻辑
void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
MessageBox.Show(“谁tmd惹祸了:” + e.Exception.Message);
//处理完后,我们需要将Handler=true表示已此异常已处理过
e.Handled = true;
}
9.控件生命周期时间触发顺序。