c# - Windows Phone 8.1 导航到同一页面的新实例

标签 c# windows-phone-8 navigation windows-phone-8.1

我有 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

How to cache a page in Windows Phone 8.1

关于c# - Windows Phone 8.1 导航到同一页面的新实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26514928/

相关文章:

c# - 将字符串转换为页面类型?

c# - Java RSA 加密

c# - 如何提高 FlowDocumentScrollViewer 的性能?

c# - WP8 应用程序中的付款

html - 单击导航中的链接时如何使 div 出现

c# - Entity Framework 4.0 - 包括实体 - 预加载问题

c# - Ajax 刷新/更新从 GridView 列到 TextBox 的总和

c# - 如何在 PuppeteerSharp 中设置下载行为?

windows-phone-8 - 在 WP8 上实现滑动事件

c# - 如何设置网格背景