我有 Windows Phone 8.1 应用程序,我需要从一个页面导航到同一页面的新实例。 例如:
Main Page -> Canvas-?
其中“ Canvas ”- xaml 页面, “?” - 实例指南。 我知道在 wp8 中它是这样的:
NavigationService.Navigate(new Uri("/Canvas.xaml?guid=" + myGuid, UriKind.Relative));
我在 MainPage.cs.Xaml 中编写这段代码:
private void addNewCanvas_Click(object sender, RoutedEventArgs e)
{
string cnvsGuid = Guid.NewGuid().ToString();
System.Diagnostics.Debug.WriteLine(cnvsGuid);
tabs.Add(new CanvasTab(){label=String.Format("New Canvas {0}",countOfOpenTabs),canvasGuid=cnvsGuid});
countOfOpenTabs++;
this.tabsGrid.ItemsSource=null;
this.tabsGrid.ItemsSource=tabs;
System.Diagnostics.Debug.WriteLine(tabsGrid.Items.Count());
this.Frame.Navigate(typeof(Canvas),cnvsGuid);
}
private void tabsGrid_ItemClick(object sender, ItemClickEventArgs e)
{
this.Frame.Navigate(typeof(Canvas), ((CanvasTab)e.ClickedItem).canvasGuid);
System.Diagnostics.Debug.WriteLine(((CanvasTab)e.ClickedItem).canvasGuid);
}
private void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
if (e.PageState != null)
{
this.tabs = e.PageState["tabs"] as List<CanvasTab>;
this.tabsGrid.ItemsSource = null;
this.tabsGrid.ItemsSource = tabs;
}
}
private void navigationHelper_SaveState(object sender, SaveStateEventArgs e)
{
e.PageState["tabs"] = tabs;
}
并将此代码添加到 MainPage 的构造函数中:
this.NavigationCacheMode = NavigationCacheMode.Enabled;
this.navigationHelper = new NavigationHelper(this);
this.navigationHelper.LoadState += this.navigationHelper_LoadState;
this.navigationHelper.SaveState += this.navigationHelper_SaveState;
并在 Canvas.cs.xaml 中编写代码:
private void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
if (e.PageState != null)
{
UIElementCollection canvasItems = e.PageState["canvasItems"] as UIElementCollection;
foreach (UIElement itm in canvasItems)
{
this.mainCanvas.Children.Add(itm);
}
this.mainCanvas.UpdateLayout();
}
}
private void navigationHelper_SaveState(object sender, SaveStateEventArgs e)
{
e.PageState["canvasItems"] = this.mainCanvas.Children;
}
然后在 Canvas 的构造函数中编写这段代码:
this.navigationHelper = new NavigationHelper(this);
this.navigationHelper.LoadState += this.navigationHelper_LoadState;
this.navigationHelper.SaveState += this.navigationHelper_SaveState;
this.NavigationCacheMode = NavigationCacheMode.Required;
而且它始终只为所有 Guid 缓存一个 Canvas 。
最佳答案
尝试在您的构造函数
中使用NavigationCacheMode.Required
。
关于c# - Windows Phone 8.1 导航到同一页面的新实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26514928/