现在为止只不过对Windows Phone中的MainPage.xaml有所了解,项目中还有一个App.xaml没有看。既然MainPage是与页面内容相关,那么App肯定是与整个应用有关。而且App也是XAML类型的文件,它和MainPage.xaml结构应该类似,之前的研究也能派上用场。
<Application
x:Class="HelloWindowsPhone.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone">
<!--Application Resources-->
<Application.Resources>
</Application.Resources>
<Application.ApplicationLifetimeObjects>
<!--Required object that handles lifetime events for the application-->
<shell:PhoneApplicationService
Launching="Application_Launching" Closing="Application_Closing"
Activated="Application_Activated" Deactivated="Application_Deactivated"/>
</Application.ApplicationLifetimeObjects>
</Application>
App.xaml中的内容很少,前面部分在上一篇中已经介绍过,主要看Application.Resources和Application.ApplicationLifetimeObjects。从注释就能够看出,Application.Resources负责应用的各种资源,Application.ApplicationLifetimeObjects对应用的生命周期事件(启动、关闭、激活和释放)进行处理。
由于Hello Windows Phone特别简单,没有用到其他的资源,所以Application.Resources里的内容是空的,关于这个属性的详细介绍可参照
http://msdn.microsoft.com/en-us/library/system.windows.application.resources(v=VS.95).aspx
里面有应用实例,这里只记着可以管理资源,等到需要时再仔细研究。
Application.ApplicationLifetimeObjects中的shell:PhoneApplicationService负责应用的生命周期事件,它的事件类似MainPage.xaml中Button的Click事件。既然Click事件在MainPage.xaml.cs中有响应函数ShowMessage_Click (object sender, RoutedEventArgs e),那么这四个生命周期事件在App.xaml.cs中肯定也有自己的响应函数。接下来就转到App.cs中看一看。
通过函数名称很容易就在App.xaml.cs中找到对应的四个响应函数:
// Code to execute when theapplication is launching (eg, from Start)
// This codewill not execute when the application is reactivated
privatevoid Application_Launching(object sender, LaunchingEventArgse)
{
}
// Code toexecute when the application is activated (brought to foreground)
// This codewill not execute when the application is first launched
privatevoid Application_Activated(object sender, ActivatedEventArgse)
{
}
// Code toexecute when the application is deactivated (sent to background)
// This codewill not execute when the application is closing
privatevoid Application_Deactivated(object sender, DeactivatedEventArgse)
{
}
// Code toexecute when the application is closing (eg, user hit Back)
// This codewill not execute when the application is deactivated
privatevoid Application_Closing(object sender, ClosingEventArgse)
{
}
不过这四个什么都没有做,看注释能够了解一些基本信息,对应用的生命周期有个大概了解。
App.xaml.cs是HelloWindows Phone中最长的一个文件,不过它的注释也是最多的,通过注释和函数名称就能对App类有很好的理解。
首先是类成员变量:
public PhoneApplicationFrameRootFrame { get; privateset; }
它是应用的根框架。
接着就是App类的构造函数:
public App()
{
// Global handler for uncaught exceptions.
UnhandledException += Application_UnhandledException;
// Standard Silverlight initialization
InitializeComponent();
// Phone-specific initialization
InitializePhoneApplication();
// Show graphics profiling information while debugging.
if (System.Diagnostics.Debugger.IsAttached)
{
// Display the current frame rate counters.
Application.Current.Host.Settings.EnableFrameRateCounter = true;
// Show the areas of the app that are being redrawn in each frame.
//Application.Current.Host.Settings.EnableRedrawRegions = true;
// Enable non-production analysis visualization mode,
// which shows areas of a page that are handed off to GPU with a colored overlay.
//Application.Current.Host.Settings.EnableCacheVisualization = true;
// Disable the application idle detection by setting the UserIdleDetectionMode property of the
// application's PhoneApplicationService object to Disabled.
// Caution:- Use this under debug mode only. Application that disables user idle detection will continue to run
// and consume battery power when the user is not using the phone.
PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;
}
}
构造函数首先添加异常处理函数Application_UnhandledException:
private voidApplication_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
if(System.Diagnostics.Debugger.IsAttached)
{
// Anunhandled exception has occurred; break into the debugger
System.Diagnostics.Debugger.Break();
}
}
App构造函数接着调用InitializeComponent,是不是看着有点眼熟?在之前的“心有灵犀”中见过它,不用看定义,它里面肯定包括载入App.xaml的内容。验证一下吧!右击函数名InitializeComponent,选择Go to definition,跳转到App.g.i.cs文件。InitializeComponent的定义如下:
public void InitializeComponent() {
if(_contentLoaded) {
return;
}
_contentLoaded = true;
System.Windows.Application.LoadComponent(this,new System.Uri("/HelloWindowsPhone;component/App.xaml",System.UriKind.Relative));
}
里面果然载入了App.xaml文件。
现在来看,App的一系列文件与MainPage一系列文件是相似的,都是一个模式,下一篇继续分析App.xaml的剩余精彩部分。