Windows Phone 开发学习笔记(七) Hello Windows Phone之有始有终

本篇继续分析App构造函数。

在Silverlight完成初始化后,App构造函数调用InitializePhoneApplication:

 private voidInitializePhoneApplication()
        {
            if(phoneApplicationInitialized)
                return;
 
            // Createthe frame but don't set it as RootVisual yet; this allows the splash
            // screento remain active until the application is ready to render.
            RootFrame = new PhoneApplicationFrame();
            RootFrame.Navigated +=CompleteInitializePhoneApplication;
 
            // Handlenavigation failures
            RootFrame.NavigationFailed +=RootFrame_NavigationFailed;
 
            // Ensurewe don't initialize again
            phoneApplicationInitialized = true;
        }

这个函数创建根框架实例,并添加事件处理函数,分别对初始化成功和失败进行处理。成功则调用CompleteInitializePhoneApplication,失败则调用RootFrame_NavigationFailed。注意RootFrame的Navigated属性,转到它的定义可以看到注释:当导航到当前内容页面且内容页面可用时触发该事件。而在Navigated属性下面还有Navigating属性,它的注释是:当有导航请求时触发该事件。通过对这个两个属性进行处理,我们能够自定义页面间的跳转。RootFrame_NavigationFailed是导航失败时调用的默认异常处理,不详细看。主要看下CompleteInitializePhoneApplication:

private voidCompleteInitializePhoneApplication(objectsender, NavigationEventArgs e)
        {
            // Setthe root visual to allow the application to render
            if(RootVisual != RootFrame)
                RootVisual = RootFrame;
 
            // Removethis handler since it is no longer needed
            RootFrame.Navigated -=CompleteInitializePhoneApplication;
        }

RootVisual没有看到过,查看下它的定义可以知道,它是Application类的成员,代表程序的主界面。因为创建RootFrame需要时间,所以在启动应用时,首先会出现载入页面,过一会儿才会导航到应用程序主页面。既然这样,我跳过等待载入这一步,直接将RootFrame显示出来会有什么结果?

注释掉

RootFrame.Navigated+= CompleteInitializePhoneApplication;

在其后添加

RootVisual =RootFrame;

编译运行。可以看到应用直接显示主页面,没有显示载入页面,暂时没有什么问题,不过效果不太好,还是用默认的方式吧。而且注释里面也提到了:不要在这个方法里添加任何代码!

在InitializePhoneApplication完成后,整个应用就已经能够正常运行,不过为了方便调试,App构造函数里还增加了几种调试模式供选择。

// Show graphics profilinginformation 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 UserIdleDetectionModeproperty of the
                //application's PhoneApplicationService object to Disabled.
                //Caution:- Use this under debug mode only. Application that disables user idledetection will continue to run
                //and consume battery power when the user is not using the phone.
               PhoneApplicationService.Current.UserIdleDetectionMode= IdleDetectionMode.Disabled;
            }

从注释里就能知道这几种模式的作用,利用这几个模式能对应用的运行状态有所了解,下面是使用不同模式的显示结果:


MSDN上已经对Windows Phone应用的生命周期做了很好的说明

http://msdn.microsoft.com/zh-cn/library/ff817008(v=vs.92)


至此,整个App.xaml已经分析完。一个简单的HelloWindows Phone程序,其内容还是很多的。感觉这些是Windows Phone开发的基础,多研究点对以后开发应该也有帮助,至少能把握整体结构,也方便调试。

 

你可能感兴趣的:(Windows,Phone)