WP8: quick application resumption + secondary tile + MainPage = 2 instances

I have the same problem:

http://social.msdn.microsoft.com/Forums/wpapps/en-us/af8615e7-8e90-4069-aa4d-3c4a84a6a3d0/windows-phone-8-fast-app-resume-with-deeplinks?forum=wpdevelop

I am not an expert in C # or WP, so please bear with me.

  • I have secondary tiles that reference "/MainPage.xaml?id=XX".
  • I have a quick application renewal. (ActivationPolicy = "Resume" in the application manifest)
  • I have only one page in my application: MainPage.xaml.

Problem . When I resume the application using the secondary tile ("/MainPage.xaml?id=XX"), I get a brief overview of the previous instance (which would resume) and then MainPage is initialized again, creating a new instance. In fact, the application loads from scratch after I looked at what was previously open.

This is clearly undesirable behavior. I want to use an existing instance to accomplish my task.


Attempt 1 : Use e.Cancel = true;to cancel navigation to MainPage.xaml:
(using the App.xaml.cs code from the official quick sample application summary example to determine how the application was launched)

...
else if (e.NavigationMode == NavigationMode.New && wasRelaunched)
{
  // This block will run if the previous navigation was a relaunch
  wasRelaunched = false;

  if (e.Uri.ToString().Contains("="))
  {
    // This block will run if the launch Uri contains "=" (ex: "id=XX") which
    // was specified when the secondary tile was created in MainPage.xaml.cs
    sessionType = SessionType.DeepLink;

    e.Cancel = true; // <======================== Here

    // The app was relaunched via a Deep Link.
    // The page stack will be cleared.
  }
}
...

. OnNavigatedTo , .

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
  String navId;
  if (e.NavigationMode != System.Windows.Navigation.NavigationMode.Back)
  {
    if (NavigationContext.QueryString.TryGetValue("id", out navId))
    {
      MessageBox.Show(navId.ToString()); // Not reached
    }
  }
  ...

2: e.Cancel = true;, MainPage.xaml, Uri MainPage:

// App.xaml.cs
...
else if (e.NavigationMode == NavigationMode.New && wasRelaunched)
{
  // This block will run if the previous navigation was a relaunch
  wasRelaunched = false;

  if (e.Uri.ToString().Contains("="))
  {
    // This block will run if the launch Uri contains "=" (ex: "id=XX") which
    // was specified when the secondary tile was created in MainPage.xaml.cs
    sessionType = SessionType.DeepLink;

    e.Cancel = true;

    MainPage.GoToDeepLink(e.Uri); // <======================== Here

    // The app was relaunched via a Deep Link.
    // The page stack will be cleared.
  }
}
...

// MainPage.xaml.cs
public static void GoToDeepLink(Uri uri) // <======================== Here
{
  // Convert the uri into a list and navigate to it.
  string path = uri.ToString();
  string id = path.Substring(path.LastIndexOf('=') + 1);

  MyList list = App.ViewModel.ListFromId(Convert.ToInt32(id));
  pivotLists.SelectedItem = list;
}

. , pivotLists . , , , MainPage (MainPage newMainPage = new MainPage();) newMainPage.pivotLists.SelectedItem = list; - , newMainPage one/replace ... -, / /.


, , . , , , .

.

+3
1

, , MainPage ( ). , ​​:

app.xaml.cs:

, , MainPage - ​​ , MainPage

public static bool returnPage = false;

RootFrame_Navigating true:

// ...
else if (e.NavigationMode == NavigationMode.New && wasRelaunched)
{
   // This block will run if the previous navigation was a relaunch
   wasRelaunched = false;
   returnPage = true;
// ...

ClearBackStackAfterReset - :

// ...
if (e.NavigationMode != NavigationMode.New || returnPage)
      return;
// ...

MainPage.cs:

, :

public MainPage()
{
  if (!App.returnPage)
     InitializeComponent();
}

MainPage , - ​​ , :

private static string navId = "";

​​ - OnNavigatedTo:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
  if (App.returnPage)
  {
     App.returnPage = false;
     NavigationContext.QueryString.TryGetValue("id", out navId);
     NavigationService.GoBack();
  }
  else if (e.NavigationMode != NavigationMode.Reset)
  {
     // normal navigation
  }
}

:

  • , returnPage ,
  • , :

    1. NavigationMode.Reset - , - .
    2. MainPage, returnPage true, - if InitializeComponent . OnNavigatedTo Navigates MainPage -
    3. , MainPage NavigationMode.Back, , .

: - - , ( , isRelunched ..) - , . - - , , Tombstone.

, .

+1

All Articles