快速应用程序恢复不起作用

时间:2014-12-19 14:14:58

标签: c# windows-phone-8 windows-phone windows-phone-8.1

我正在尝试为Windows Phone 8实施快速应用简历。我跟着the link at MSDN

以下是XAML中的代码:

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

这是app.xaml.cs中的代码

    public static PhoneApplicationFrame RootFrame { get; private set; }
    bool wasRelaunched = false;      
    bool mustClearPagestack = false;      
    IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

    enum SessionType
    {
        None,
        Home,
        DeepLink
    }

    private SessionType sessionType = SessionType.None;

    public App()
    {           
        UnhandledException += Application_UnhandledException;         
        InitializeComponent();           
        InitializePhoneApplication();        
        InitializeLanguage();       
        if (Debugger.IsAttached)
        {               
            Application.Current.Host.Settings.EnableFrameRateCounter =true;  
           PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;
        }
    }

    private void Application_Launching(object sender, LaunchingEventArgs e)
    {
        RemoveCurrentDeactivationSettings();
    }

    private void Application_Activated(object sender, ActivatedEventArgs e)
    {

        mustClearPagestack = CheckDeactivationTimeStamp();

        if (!e.IsApplicationInstancePreserved)
        {
            RestoreSessionType();
        }
    }

     private void Application_Deactivated(object sender, DeactivatedEventArgs e)
     {
        SaveCurrentDeactivationSettings();
    }

    private void Application_Closing(object sender, ClosingEventArgs e)
    {
        RemoveCurrentDeactivationSettings();
    }

    private void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
    {
        if (Debugger.IsAttached)
        {

            Debugger.Break();
        }
    }

    private void Application_UnhandledException(object sender,ApplicationUnhandledExceptionEventArgs e)
    {
        if (Debugger.IsAttached)
        {

            Debugger.Break();
        }
    }

    private bool phoneApplicationInitialized = false;

    private void InitializePhoneApplication()
    {
        if (phoneApplicationInitialized)
            return;


        RootFrame= new PhoneApplicationFrame();

        RootFrame.Background = new SolidColorBrush(Color.FromArgb(255, 27, 200, 174));
        RootFrame.Navigated += CompleteInitializePhoneApplication;


        RootFrame.NavigationFailed += RootFrame_NavigationFailed;


        RootFrame.Navigated += CheckForResetNavigation;
        RootFrame.Navigating += RootFrame_Navigating;


        phoneApplicationInitialized = true;
    }

    void RootFrame_Navigating(object sender, NavigatingCancelEventArgs e)
    {
        if (e.Uri.ToString().Contains(@"/MainPage.xaml") == true && !AppPrefManager.Instance.IsFastAppResumeEnabled)
        {
            RootFrame.Dispatcher.BeginInvoke(delegate
            {
                if (!AppPrefManager.Instance.IsVirginLaunchCompleted)
                {
                    RootFrame.Navigate(new Uri(Constants.kIntroPage, UriKind.Relative));
                }
                else
                {
                    RootFrame.Navigate(new Uri(Constants.kMainPage, UriKind.Relative));
                }
            });
            e.Cancel = true;
        }


        if (sessionType == SessionType.None && e.NavigationMode == NavigationMode.New)
        {

            if (e.Uri.ToString().Contains("DeepLink=true"))
            {
                sessionType = SessionType.DeepLink;
            }
            else if (e.Uri.ToString().Contains("/MainPage.xaml"))
            {
                sessionType = SessionType.Home;
            }
        }


        if (e.NavigationMode == NavigationMode.Reset)
        {

            wasRelaunched = true;
        }
        else if (e.NavigationMode == NavigationMode.New && wasRelaunched)
        {

            wasRelaunched = false;

            if (e.Uri.ToString().Contains("DeepLink=true"))
            {


                sessionType = SessionType.DeepLink;

            }
            else if (e.Uri.ToString().Contains("/MainPage.xaml"))
            {

                if (sessionType == SessionType.DeepLink)
                {

                    sessionType = SessionType.Home;
                }
                else
                {
                    if (!mustClearPagestack)
                    {

                        e.Cancel = true;
                        RootFrame.Navigated -= ClearBackStackAfterReset;
                    }
                }
            }

            mustClearPagestack = false;
        }
    }

    private void CompleteInitializePhoneApplication(object sender, NavigationEventArgs e)
    {

        if (RootVisual != RootFrame)
            RootVisual = RootFrame;

        RootFrame.Navigated -= CompleteInitializePhoneApplication;
    }

    private 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 && e.NavigationMode != NavigationMode.Refresh)
            return;


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

     private void InitializeLanguage()
    {
        try
        {
            FlowDirection flow = (FlowDirection)Enum.Parse(typeof(FlowDirection), AppResources.ResourceFlowDirection);
            RootFrame.FlowDirection = flow;
        }
        catch
        {
            if (Debugger.IsAttached)
            {
                Debugger.Break();
            }

            throw;
        }
    }

    bool CheckDeactivationTimeStamp()
    {
        DateTimeOffset lastDeactivated;

        if (settings.Contains("DeactivateTime"))
        {
            lastDeactivated = (DateTimeOffset)settings["DeactivateTime"];
        }

        var currentDuration = DateTimeOffset.Now.Subtract(lastDeactivated);

        return TimeSpan.FromSeconds(currentDuration.TotalSeconds) > TimeSpan.FromSeconds(30);
    }

    public bool AddOrUpdateValue(string Key, Object value)
    {
        bool valueChanged = false;


        if (settings.Contains(Key))
        {

            if (settings[Key] != value)
            {

                settings[Key] = value;
                valueChanged = true;
            }
        }

        else
        {
            settings.Add(Key, value);
            valueChanged = true;
        }
        return valueChanged;
    }

    public void RemoveValue(string Key)
    {

        if (settings.Contains(Key))
        {
            settings.Remove(Key);
        }
    }

    public void SaveCurrentDeactivationSettings()
    {
        if (AddOrUpdateValue("DeactivateTime", DateTimeOffset.Now))
        {
            settings.Save();
        }

        if (AddOrUpdateValue("SessionType", sessionType))
        {
            settings.Save();
        }

    }

    public void RemoveCurrentDeactivationSettings()
    {
        RemoveValue("DeactivateTime");
        RemoveValue("SessionType");
        settings.Save();
    }

    void RestoreSessionType()
    {
        if (settings.Contains("SessionType"))
        {
            sessionType = (SessionType)settings["SessionType"];
        }
    }

假设我在ThirdPage中。我按下Windows按钮。然后我从开始屏幕按我的应用程序图标。而不是从第三页恢复应用程序。它首先显示ThirdPage,然后从MainPage开始。

2 个答案:

答案 0 :(得分:1)

默认情况下,当应用程序通过应用磁贴启动时,应用仍然会导航到默认页面。 如果您愿意,可以在RootFrame_Navigated方法中检查会话类型并取消该导航。

默认模板在app.xaml.cs中添加了一个 CheckNavigation 方法,该方法在使用NavigationMode 重置导航后清除了后台堆栈。

您可以在那里查看,您的应用应该留在最后一页,还是最好重置并重新开始。

可以在此处找到处理不同激活类型的示例: MSDN Fast Resume Sample, App.xaml.cs

(方法:RootFrame_Navigated)

答案 1 :(得分:0)

列出的代码会搞砸。它与评论不符。 查看RootFrame_Navigating - 底部的mustClearPagesStack设置为false - 但查看原始MSDN链接中的注释 - 它上面的两个位置表示必须清除页面堆栈....但是因为该标志设置为false所以它是混乱的起来。因此,在顶部将标志设置为false,但是在它所说的两个“if conditions”中将其设置为true。 然后它会像冠军一样工作。