2

I have Windows Phone 8.1 app and I need to navigate from a page to a new instance of the same page. For example:

Main Page -> Canvas-?

Where 'Canvas' - xaml page, '?' - guid of instance. I know that in wp8 it is going like this:

NavigationService.Navigate(new Uri("/Canvas.xaml?guid=" + myGuid, UriKind.Relative));

I write this code in 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;
}

And Add This Code to constructor of MainPage:

this.NavigationCacheMode = NavigationCacheMode.Enabled;
this.navigationHelper = new NavigationHelper(this);
this.navigationHelper.LoadState += this.navigationHelper_LoadState;
this.navigationHelper.SaveState += this.navigationHelper_SaveState;

And write code in 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;
}

And write this code in constructor of Canvas:

this.navigationHelper = new NavigationHelper(this);
this.navigationHelper.LoadState += this.navigationHelper_LoadState;
this.navigationHelper.SaveState += this.navigationHelper_SaveState;
this.NavigationCacheMode = NavigationCacheMode.Required;

And it always only caching only one canvas for all Guids.

awesoon
  • 32,469
  • 11
  • 74
  • 99
earthUser
  • 21
  • 3
  • did you try this.Frame.Navigate(typeof(Canvas), myGuid)? – Michał Żołnieruk Oct 23 '14 at 00:43
  • The cache logic in WinRT is somewhat strange, if you set the caching to required then the same page instance of one type is reused, if you disable the cache then a new instance is created but the page is not cached, check out this question: http://stackoverflow.com/questions/11539755/winrt-frame-and-page-caching-how-to-create-new-page-instance-on-navigate-and/12730975#12730975 – Rico Suter Jan 22 '15 at 12:12
  • 1
    I reimplemented all the caching relevang classes so that the caching mechanism is much more flexible, check out my library: https://mytoolkit.codeplex.com/wikipage?title=Paging – Rico Suter Jan 22 '15 at 12:13

1 Answers1

0

Try using NavigationCacheMode.Required within your constructor.

How to cache a page in Windows Phone 8.1

Community
  • 1
  • 1
Kulasangar
  • 9,046
  • 5
  • 51
  • 82