本人不才学过一点点WinForm,现在正在利用非工作时间学习WPF,可惜没有任性,时不时的偷懒、不看书也不更新博客,嘿嘿…大侠见笑。
下面是我的一个关于Application类的一个小Practice(当然、还没有做完、后续更新…)
XAML部分代码
<
Application
x:Class
="ApplicationDemo.App"
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri ="MainWindow.xaml" Activated ="Application_Activated"
Deactivated ="Application_Deactivated" Exit ="Application_Exit"
Startup ="Application_Startup" SessionEnding ="Application_SessionEnding" >
< Application.Resources >
</ Application.Resources >
</ Application >
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri ="MainWindow.xaml" Activated ="Application_Activated"
Deactivated ="Application_Deactivated" Exit ="Application_Exit"
Startup ="Application_Startup" SessionEnding ="Application_SessionEnding" >
< Application.Resources >
</ Application.Resources >
</ Application >
代码
///
<summary>
/// App.xaml 的交互逻辑
/// </summary>
public partial class App : Application
{
#region Application 类中的普通事件处理
/// <summary>
/// 应用程序成为了前台应用程序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Application_Activated( object sender, EventArgs e)
{
Console.WriteLine( " WPF应用程序成为了前台应用程序 " );
}
/// <summary>
/// 应用程序停止成为了前台应用程序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Application_Deactivated( object sender, EventArgs e)
{
Console.WriteLine( " WPF应用程序 停止 成为了前台应用程序 " );
}
/// <summary>
/// 应用程序退出,但此时已经无法停止应用程序的退出
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Application_Exit( object sender, ExitEventArgs e)
{
MessageBox.Show( " 应用程序将要退出,但此时已经无法停止应用程序的退出 " );
}
/// <summary>
/// 用户注销或者关闭了操作系统
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Application_SessionEnding( object sender, SessionEndingCancelEventArgs e)
{
MessageBox.Show( " 用户注销或者关闭了操作系统! " );
}
/// <summary>
/// WPF应用程序调用了System.Windows.Application.Run() 方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Application_Startup( object sender, StartupEventArgs e)
{
MessageBox.Show( " WPF应用程序调用了System.Windows.Application.Run() 方法 " );
}
#endregion
#region Application类元数据
#region 属性
/// / 摘要:
/// / 获取当前 System.AppDomain 的 System.Windows.Application 对象。
/// /
/// / 返回结果:
/// / 当前 System.AppDomain 的 System.Windows.Application 对象。
// public static Application Current { get; }
/// /
/// / 摘要:
/// / 获取或设置应用程序的主窗口。
/// /
/// / 返回结果:
/// / 一个指定为应用程序主窗口的 System.Windows.Window。
/// /
/// / 异常:
/// / System.InvalidOperationException:
/// / System.Windows.Application.MainWindow 是从浏览器承载的应用程序(如 XAML browser applications
/// / (XBAPs))设置的。
// public Window MainWindow { get; set; }
/// /
/// / 摘要:
/// / 获取应用程序范围属性的集合。
/// /
/// / 返回结果:
/// / 一个包含应用程序范围的属性的 System.Collections.IDictionary。
// public IDictionary Properties { get; }
/// /
/// / 摘要:
/// / 获取或设置为 WPF 应用程序中的资源提供包uniform resource identifiers (URIs) 的 System.Reflection.Assembly。
/// /
/// / 返回结果:
/// / 对为 WPF 应用程序中的资源提供包uniform resource identifiers (URIs) 的 System.Reflection.Assembly
/// / 的引用。
/// /
/// / 异常:
/// / System.InvalidOperationException:
/// / WPF 应用程序具有入口程序集,或者已设置 System.Windows.Application.ResourceAssembly。
// public static Assembly ResourceAssembly { get; set; }
/// /
/// / 摘要:
/// / 获取或设置应用程序范围资源(如样式和画笔)的集合。
/// /
/// / 返回结果:
/// / 一个包含零个或多个应用程序范围资源的 System.Windows.ResourceDictionary 对象。
// [Ambient]
// public ResourceDictionary Resources { get; set; }
/// /
/// / 摘要:
/// / 获取或设置导致调用 System.Windows.Application.Shutdown() 方法的条件。
/// /
/// / 返回结果:
/// / 一个 System.Windows.ShutdownMode 枚举值。默认值为 System.Windows.ShutdownMode.OnLastWindowClose。
// public ShutdownMode ShutdownMode { get; set; }
/// /
/// / 摘要:
/// / 获取或设置一个在应用程序启动时自动显示的 UI。
/// /
/// / 返回结果:
/// / 一个 System.Uri,引用在应用程序启动时自动打开的 UI。
/// /
/// / 异常:
/// / System.ArgumentNullException:
/// / System.Windows.Application.StartupUri 设置为 null 值。
// public Uri StartupUri { get; set; }
/// /
/// / 摘要:
/// / 获取应用程序中的实例化窗口。
/// /
/// / 返回结果:
/// / 一个 System.Windows.WindowCollection,包含对当前 System.AppDomain 中的所有窗口对象的引用。
// public WindowCollection Windows { get; }
#endregion
#region 事件 许多没有列出 对应的方法也没有列出
/// / 摘要:
/// / 当应用程序成为前台应用程序时发生。
// public event EventHandler Activated;
/// /
/// / 摘要:
/// / 当应用程序停止作为前台应用程序时发生。
// public event EventHandler Deactivated;
/// /
/// / 摘要:
/// / 在异常由应用程序引发但未进行处理时发生。
// public event DispatcherUnhandledExceptionEventHandler DispatcherUnhandledException;
/// /
/// / 摘要:
/// / 恰好在应用程序关闭之前发生,且无法取消。
// public event ExitEventHandler Exit;
/// /
/// / 摘要:
/// / 在用户通过注销或关闭操作系统而结束 Windows 会话时发生。
// public event SessionEndingCancelEventHandler SessionEnding;
/// /
/// / 摘要:
/// / 在调用 System.Windows.Application 对象的 System.Windows.Application.Run() 方法时发生。
// public event StartupEventHandler Startup;
#endregion
#region 一些方法
/// /
/// / 摘要:
/// / 关闭一个应用程序。
// [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
// public void Shutdown();
/// /
/// / 摘要:
/// / 关闭将指定退出代码返回给操作系统的应用程序。
/// /
/// / 参数:
/// / exitCode:
/// / 应用程序的一个整数退出代码。默认退出代码为 0。
// [SecurityCritical]
// public void Shutdown(int exitCode);
#endregion
#endregion
}
/// App.xaml 的交互逻辑
/// </summary>
public partial class App : Application
{
#region Application 类中的普通事件处理
/// <summary>
/// 应用程序成为了前台应用程序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Application_Activated( object sender, EventArgs e)
{
Console.WriteLine( " WPF应用程序成为了前台应用程序 " );
}
/// <summary>
/// 应用程序停止成为了前台应用程序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Application_Deactivated( object sender, EventArgs e)
{
Console.WriteLine( " WPF应用程序 停止 成为了前台应用程序 " );
}
/// <summary>
/// 应用程序退出,但此时已经无法停止应用程序的退出
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Application_Exit( object sender, ExitEventArgs e)
{
MessageBox.Show( " 应用程序将要退出,但此时已经无法停止应用程序的退出 " );
}
/// <summary>
/// 用户注销或者关闭了操作系统
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Application_SessionEnding( object sender, SessionEndingCancelEventArgs e)
{
MessageBox.Show( " 用户注销或者关闭了操作系统! " );
}
/// <summary>
/// WPF应用程序调用了System.Windows.Application.Run() 方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Application_Startup( object sender, StartupEventArgs e)
{
MessageBox.Show( " WPF应用程序调用了System.Windows.Application.Run() 方法 " );
}
#endregion
#region Application类元数据
#region 属性
/// / 摘要:
/// / 获取当前 System.AppDomain 的 System.Windows.Application 对象。
/// /
/// / 返回结果:
/// / 当前 System.AppDomain 的 System.Windows.Application 对象。
// public static Application Current { get; }
/// /
/// / 摘要:
/// / 获取或设置应用程序的主窗口。
/// /
/// / 返回结果:
/// / 一个指定为应用程序主窗口的 System.Windows.Window。
/// /
/// / 异常:
/// / System.InvalidOperationException:
/// / System.Windows.Application.MainWindow 是从浏览器承载的应用程序(如 XAML browser applications
/// / (XBAPs))设置的。
// public Window MainWindow { get; set; }
/// /
/// / 摘要:
/// / 获取应用程序范围属性的集合。
/// /
/// / 返回结果:
/// / 一个包含应用程序范围的属性的 System.Collections.IDictionary。
// public IDictionary Properties { get; }
/// /
/// / 摘要:
/// / 获取或设置为 WPF 应用程序中的资源提供包uniform resource identifiers (URIs) 的 System.Reflection.Assembly。
/// /
/// / 返回结果:
/// / 对为 WPF 应用程序中的资源提供包uniform resource identifiers (URIs) 的 System.Reflection.Assembly
/// / 的引用。
/// /
/// / 异常:
/// / System.InvalidOperationException:
/// / WPF 应用程序具有入口程序集,或者已设置 System.Windows.Application.ResourceAssembly。
// public static Assembly ResourceAssembly { get; set; }
/// /
/// / 摘要:
/// / 获取或设置应用程序范围资源(如样式和画笔)的集合。
/// /
/// / 返回结果:
/// / 一个包含零个或多个应用程序范围资源的 System.Windows.ResourceDictionary 对象。
// [Ambient]
// public ResourceDictionary Resources { get; set; }
/// /
/// / 摘要:
/// / 获取或设置导致调用 System.Windows.Application.Shutdown() 方法的条件。
/// /
/// / 返回结果:
/// / 一个 System.Windows.ShutdownMode 枚举值。默认值为 System.Windows.ShutdownMode.OnLastWindowClose。
// public ShutdownMode ShutdownMode { get; set; }
/// /
/// / 摘要:
/// / 获取或设置一个在应用程序启动时自动显示的 UI。
/// /
/// / 返回结果:
/// / 一个 System.Uri,引用在应用程序启动时自动打开的 UI。
/// /
/// / 异常:
/// / System.ArgumentNullException:
/// / System.Windows.Application.StartupUri 设置为 null 值。
// public Uri StartupUri { get; set; }
/// /
/// / 摘要:
/// / 获取应用程序中的实例化窗口。
/// /
/// / 返回结果:
/// / 一个 System.Windows.WindowCollection,包含对当前 System.AppDomain 中的所有窗口对象的引用。
// public WindowCollection Windows { get; }
#endregion
#region 事件 许多没有列出 对应的方法也没有列出
/// / 摘要:
/// / 当应用程序成为前台应用程序时发生。
// public event EventHandler Activated;
/// /
/// / 摘要:
/// / 当应用程序停止作为前台应用程序时发生。
// public event EventHandler Deactivated;
/// /
/// / 摘要:
/// / 在异常由应用程序引发但未进行处理时发生。
// public event DispatcherUnhandledExceptionEventHandler DispatcherUnhandledException;
/// /
/// / 摘要:
/// / 恰好在应用程序关闭之前发生,且无法取消。
// public event ExitEventHandler Exit;
/// /
/// / 摘要:
/// / 在用户通过注销或关闭操作系统而结束 Windows 会话时发生。
// public event SessionEndingCancelEventHandler SessionEnding;
/// /
/// / 摘要:
/// / 在调用 System.Windows.Application 对象的 System.Windows.Application.Run() 方法时发生。
// public event StartupEventHandler Startup;
#endregion
#region 一些方法
/// /
/// / 摘要:
/// / 关闭一个应用程序。
// [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
// public void Shutdown();
/// /
/// / 摘要:
/// / 关闭将指定退出代码返回给操作系统的应用程序。
/// /
/// / 参数:
/// / exitCode:
/// / 应用程序的一个整数退出代码。默认退出代码为 0。
// [SecurityCritical]
// public void Shutdown(int exitCode);
#endregion
#endregion
}
C#部分代码
//
摘要:
// 使用指定键搜索user interface (UI) 资源(如 System.Windows.Style 或 System.Windows.Media.Brush),并在未找到所请求的资源时引发异常(请参见Resources
// Overview)。
//
// 参数:
// resourceKey:
// 要查找的资源名。
//
// 返回结果:
// 所请求的资源对象。如果未找到所请求的资源,则引发 System.Windows.ResourceReferenceKeyNotFoundException。
//
// 异常:
// System.Windows.ResourceReferenceKeyNotFoundException:
// 无法找到资源。
public object FindResource( object resourceKey);
//
// 摘要:
// 搜索指定资源。
//
// 参数:
// resourceKey:
// 要查找的资源名。
//
// 返回结果:
// 所请求的资源对象。如果未找到所请求的资源,则返回 null 引用。
public object TryFindResource( object resourceKey);
// 使用指定键搜索user interface (UI) 资源(如 System.Windows.Style 或 System.Windows.Media.Brush),并在未找到所请求的资源时引发异常(请参见Resources
// Overview)。
//
// 参数:
// resourceKey:
// 要查找的资源名。
//
// 返回结果:
// 所请求的资源对象。如果未找到所请求的资源,则引发 System.Windows.ResourceReferenceKeyNotFoundException。
//
// 异常:
// System.Windows.ResourceReferenceKeyNotFoundException:
// 无法找到资源。
public object FindResource( object resourceKey);
//
// 摘要:
// 搜索指定资源。
//
// 参数:
// resourceKey:
// 要查找的资源名。
//
// 返回结果:
// 所请求的资源对象。如果未找到所请求的资源,则返回 null 引用。
public object TryFindResource( object resourceKey);