我在这里提出了同样的问题:
http://social.msdn.microsoft.com/forums/wpapps/en-us/af8615e7-8e7-8e90-4069-aa4d-3c44a84a6a6a3d0/= wpdevelop
我不是C#或WP专家,所以请忍受我。
- 我有次级瓷砖,它们链接到"/mainpage.xaml?id = xx"。
- 我启用了快速应用简历。(ActivationPolicy =应用清单中的"简历")
- 我的应用中只有一个页面:mainpage.xaml。
问题:当我使用辅助图块恢复应用程序("/mainpage.xaml?id=xx")时,我可以简要介绍上一个实例(本来可以恢复)和然后再次初始化主页,创建一个新实例。实际上,该应用在给我以前打开的内容后从头开始加载。
显然是不希望的行为。我想使用现有实例执行我的任务。
尝试1
:使用e.Cancel = true;
取消导航到mainpage.xaml:
(使用官方快速应用简历示例中的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; // <======================== Here
// The app was relaunched via a Deep Link.
// The page stack will be cleared.
}
}
...
问题 :这样做,我的navigigated to事件处理程序永远不会发射,所以我的查询字符串永远不会被解析。
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传递到主页中的方法:
// 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 newMainPage = new MainPage();
)并致电newMainPage.pivotLists.SelectedItem = list;
-但我不知道如何使用newmainpage而不是现有一个/替换它...或者,如果我想要/不会引起进一步的问题/并发症。
我不知道该问题是什么解决方案,我可能会朝着完全错误的方向发展。请用代码示例简单地保留所有建议,如果可以的话,我仍在学习。
感谢您的任何帮助。
看来,当您从次级瓷砖重新打开应用时,它已重新激活并创建了主页的新实例(即使先前的运行中有一个)。如果我正确理解您,我设法做了这样的事情:
在app.xaml.cs中:
我添加了一个变量,该变量指示我是否应该从次级瓷砖导航后是否应该返回以前的主页 - 它需要静态,因为我想从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();
}
在主页中,我也从次级图块传递的变量 - 它也很静态,因为我只需要一个实例:
private static string navId = "";
和技巧的核心-navigatedto:
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
}
}
它是这样的工作:
- 当您正常启动应用程序时,返回页面是错误的,一切正常
当您从次要瓷砖中激活它时,很少发生任何事情:
1。首先使用navigationmode.reset导航到您上面的页面 - 我们对此不感兴趣,所以我关闭了它 - 什么都不应发生
2.然后程序试图创建主页的新实例,但是返回页面是正确的,并且由于IF语句,initializecomponent无法运行。在此之后,在onnavigatedto中,程序保存传递的查询,并导航回主页的先前实例 - 从上一个运行
3.最后,我们正在使用NavigationMode.Back导航到正确的主页,并且我们的Querystring保存在静态变量中。
您必须意识到两件事:首先 - 可能是很少的重建(我不确定是否需要Wasrelaunched等等) - 您需要调试它,看看可以摆脱的东西。第二 - 您可能需要使用Tombstone Case测试您的应用程序。
希望这会有所帮助。