windows phone Fast Resume 快速恢复

转自:WP8点击桌面图标快速恢复应用

如何实现在WindowsPhone8 应用程序中 点击了 Windows logo 后 重新在应用程序列表中单击该程序图标 快速启动应用程序

示例代码 下载

1. 在App.cs 文件中修改添加一下代码

复制代码
   #region 电话应用程序初始化

        // 避免双重初始化
        private bool phoneApplicationInitialized = false;

        // 请勿向此方法中添加任何其他代码
        private void InitializePhoneApplication()
        {
            if (phoneApplicationInitialized)
                return;

            // 创建框架但先不将它设置为 RootVisual;这允许初始
            // 屏幕保持活动状态,直到准备呈现应用程序时。
            RootFrame = new PhoneApplicationFrame();
            RootFrame.NavigationFailed += RootFrame_NavigationFailed;

            RootFrame.NavigationFailed += RootFrame_NavigationFailed;

            RootFrame.Navigated += CompleteInitializePhoneApplication;
            RootFrame.Navigated += CheckForResetNavigation;

            RootFrame.Navigating += RootFrame_Navigating;

            // 确保我们未再次初始化
            phoneApplicationInitialized = true;
        }

        bool isReset = false;
        void RootFrame_Navigating(object sender, NavigatingCancelEventArgs e)
        {
            //Resest验证判断 (此处会执行两次)
            if (e.NavigationMode == NavigationMode.Reset)
            {
                isReset = true;
            }
            else if (e.NavigationMode == NavigationMode.New && isReset)
            {
                isReset = false;
                e.Cancel = true; // 取消导航到新页面s
                RootFrame.Navigated -= ClearBackStackAfterReset; // 取消导航完成事件(否则会直接退出程序)
            }
        }

        void CompleteInitializePhoneApplication(object sender, NavigationEventArgs e)
        {
            if (RootVisual != RootFrame)
                RootVisual = RootFrame;
            RootFrame.Navigated -= CompleteInitializePhoneApplication;
        }

        void CheckForResetNavigation(object sender, NavigationEventArgs e)
        {
            if (e.NavigationMode == NavigationMode.Reset)
                RootFrame.Navigated += ClearBackStackAfterReset;
        }
        private void ClearBackStackAfterReset(object sender, NavigationEventArgs e)
        {
            RootFrame.Navigated -= ClearBackStackAfterReset;

            if (e.NavigationMode != NavigationMode.New)
                return;

            while (RootFrame.RemoveBackEntry() != null)
            {
                ;
            }
        }

        #endregion
复制代码

2.在WMAppManifest.xml 中添加 ActivationPolicy="Resume"

  <Tasks>
      <DefaultTask Name="_default" NavigationPage="MainPage.xaml" ActivationPolicy="Resume"/>
    </Tasks>

你可能感兴趣的:(windows,phone,Fast,resume,快速恢复)