频道栏目
首页 > 资讯 > Windows Phone > 正文

windows phone 7 app.xaml启动过程

12-12-16        来源:[db:作者]  
收藏   我要投稿
在网上找了一下基本没有人分析app.xaml的执行过程.只说是整个程序的生命周期而并没有深入.
 
但是其实在app.xaml.cs的注释里面也说的很清楚了.我根据自己的理解翻译一下.
 
首先有四个事件要注意,在app.xaml文件中的Application.ApplicationLifetimeObjects节点定义
 
 
<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>
————————————————————————————–
这里多说一句,为什么在xaml文件定义的事件可以被程序知道.因为每个xaml.cs的构造函数都会调用一个InitializeComponent()函数.
这个函数是编译器自动生成的.内容如下
 
[System.Diagnostics.windowNonUserCodeAttribute()]
        public void InitializeComponent() {
            if (_contentLoaded) {
                return;
            }
            _contentLoaded = true;
            System.Windows.Application.LoadComponent(this, new System.Uri("/PhoneSample;component/App.xaml", System.UriKind.Relative));
        }
可以看到在这个函数里加载了xaml文件.如果你查看其他界面Page的InitializeComponent()函数.你可能会更明白xaml文件的作用
下面是来自Phone.Toolkit的MainPage的例子
 
 
internal System.Windows.Controls.Grid LayoutRoot;
internal System.Windows.Controls.StackPanel TitlePanel;
internal System.Windows.Controls.TextBlock ApplicationTitle;
internal System.Windows.Controls.TextBlock PageTitle;
internal System.Windows.Controls.Grid ContentPanel;
private bool _contentLoaded;
 
[System.Diagnostics.windowNonUserCodeAttribute()]
public void InitializeComponent() {
    if (_contentLoaded) {
        return;
    }
    _contentLoaded = true;
    System.Windows.Application.LoadComponent(this, new System.Uri("/PhoneToolkitSample;component/MainPage.xaml", System.UriKind.Relative));
    this.LayoutRoot = ((System.Windows.Controls.Grid)(this.FindName("LayoutRoot")));
    this.TitlePanel = ((System.Windows.Controls.StackPanel)(this.FindName("TitlePanel")));
    this.ApplicationTitle = ((System.Windows.Controls.TextBlock)(this.FindName("ApplicationTitle")));
    this.PageTitle = ((System.Windows.Controls.TextBlock)(this.FindName("PageTitle")));
    this.ContentPanel = ((System.Windows.Controls.Grid)(this.FindName("ContentPanel")));
}
————————————————————
在这里注册了程序生命周期的四个事件,Launching,Closing,Activated,Deactivated
启动程序的时候会调用Application_Launching,退出程序的时候调用Application_Closing
启动别的程序时调用Application_Deactivated,返回到本程序时调用Application_Activated
 
1.App构造函数
 
 
public App()
{
    // Global handler for uncaught exceptions.
    //注册一个全局的异常处理handle,专门处理程序中没有处理的异常.
    UnhandledException += Application_UnhandledException;
    // Show graphics profiling information while debugging.
   //下面这个是很有用的一个"宏",如果是出于调试状态则,IsAttached==true
    if (System.Diagnostics.window.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 being GPU accelerated with a colored overlay.
        //不懂
        //Application.Current.Host.Settings.EnableCacheVisualization = true;
        //在Application.Current.Host.Settings中还有其他参数.具体请参考Settings类.
    }
    // Standard Silverlight initialization
    //标准Silverlight初始化,上面已经说过了.
    InitializeComponent();
    // Phone-specific initialization
    //手机专有的初始化.后面介绍
    InitializePhoneApplication();
}
2.InitializePhoneApplication函数的作用
 
    // Avoid double-initialization
//防止2次初始化.
    private bool phoneApplicationInitialized = false;
 
    // Do not add any additional code to this method
    private void InitializePhoneApplication()
    {
        if (phoneApplicationInitialized)
            return;
 
        // Create the frame but don't set it as RootVisual yet; this allows the splash
        // screen to remain active until the application is ready to render.
    //初始化程序框架,暂时不给RootVisual复制.这样可以让splash显示,知道在程序render完成
        RootFrame = new TransitionFrame();
    //加载完成事件,在这个事件中给RootVisual=RootFrame开始显示程序界面.
        RootFrame.Navigated += CompleteInitializePhoneApplication;
 
        // Handle navigation failures
    //处理程序navigationg失败
        RootFrame.NavigationFailed += RootFrame_NavigationFailed;
 
        // Ensure we don't initialize again
    //初始化完成.不再初始化
        phoneApplicationInitialized = true;
    }
在RootFrame加载完成后,把RootFrame赋值到程序的RootVisual,这样MainPage.xaml就得到显示了.
 
为什么显示的是MainPage?
 
这是在WMAppMainfest.xml中的节点定义的.你可以换成任意你喜欢的page.
 
到这里app的启动就算完成了.
相关TAG标签
上一篇:HOWTO change ToggleSwitch’s Style on WP7
下一篇:office(word|excel|access)图标显示不正常修复
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站