1

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

我如何使用 Prism.StoreApps 实现这一目标?

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

4

1 回答 1

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);
   }
}
于 2015-05-15T17:11:14.983 回答