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

时间:2014-10-22 18:51:33

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

我有Windows Phone 8.1应用程序,我需要从一个页面导航到同一页面的新实例。 例如:

  

主页 - >帆布 - ?

'Canvas'的位置 - xaml页面, '?' - 例如guid。 我知道在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;

它总是只为所有Guids缓存一个画布。

1 个答案:

答案 0 :(得分:0)

尝试在NavigationCacheMode.Required中使用constructor

How to cache a page in Windows Phone 8.1