Windows Phone,Prism.StoreApps:暂停或终止后如何避免激活某个页面?

时间:2015-05-13 22:29:37

标签: windows-phone-8.1 prism-storeapps

我确实希望确保在应用程序导航到某个页面的情况下,该应用程序在暂停或终止后位于另一个(在我的情况下是前一个)页面上。在我的情况下,该页面用于拍照。我不希望用户在应用程序处于后台后返回此页面,因为它没有上下文信息。上下文信息位于previuos页面上。

我怎样才能用Prism.StoreApps实现这个目标?

背景:如果应用程序刚刚暂停,应用程序的状态在恢复后仍然存在,因此最后一个活动页面再次处于活动状态。我不知道如何在这种情况下设置另一个活动页面。如果应用程序被终止,Prim.StoreApps将恢复导航状态并导航到最后一个活动视图模型(因此导航到最后一个活动页面)。我不知道在这种情况下如何改变导航状态,以便导航到另一个页面。

1 个答案:

答案 0 :(得分:0)

与此同时,我喜欢自己的工作解决方案。可能不是最好的,可能有更好的解决方案,但它确实有效。

要恢复应用,我会处理Resuming事件:

private void OnResuming(object sender, object o)
{
   // Check if the current root frame contains the page we do not want to 
   // be activated after a resume
   var rootFrame = Window.Current.Content as Frame;
   if (rootFrame != null && rootFrame.CurrentSourcePageType == typeof (NotToBeResumedOnPage))
   {
      // In case the page we don't want to be activated after a resume would be activated:
      // Go back to the previous page (or optionally to another page)
      this.NavigationService.GoBack();
   }
}

对于终止后的页面恢复,我首先使用App类中的属性:

public bool MustPreventNavigationToPageNotToBeResumedOn { get; set; }

public App()
{
    this.InitializeComponent();

    // We assume that the app was restored from termination and therefore we must prevent navigation 
    // to the page that should not be navigated to after suspension and termination. 
    // In OnLaunchApplicationAsync MustPreventNavigationToPageNotToBeResumedOn is set to false since 
    // OnLaunchApplicationAsync is not invoked when the app was restored from termination.
    this.MustPreventNavigationToPageNotToBeResumedOn = true; 

    this.Resuming += this.OnResuming;
}

protected override Task OnLaunchApplicationAsync(LaunchActivatedEventArgs args)
{
   // If the application is launched normally we do not prevent navigation to the
   // page that should not be navigated to.
   this.MustPreventNavigationToPageNotToBeResumedOn = false;

   this.NavigationService.Navigate("Main", null);

   return Task.FromResult<object>(null);
}

在页面的OnNavigatedTo我不希望在简历上激活我检查此属性,只需导航回true(并将属性设置为false允许后续导航):

public override void OnNavigatedTo(object navigationParameter, NavigationMode navigationMode, 
   Dictionary<string, object> viewModelState)
{
   if (((App)Application.Current).MustPreventNavigationToPageNotToBeResumedOn)
   {
      // If must prevent navigation to this page (that should not be navigated to after 
      // suspension and termination) we reset the marker and just go back. 
      ((App)Application.Current).MustPreventNavigationToPageNotToBeResumedOn = false;
      this.navigationService.GoBack();
   }
   else
   {
      base.OnNavigatedTo(navigationParameter, navigationMode, viewModelState);
   }
}