简体   繁体   中英

Windows Phone 8.1 navigate to new instance of same page

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.

Try using NavigationCacheMode.Required within your constructor .

How to cache a page in Windows Phone 8.1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM