简体   繁体   中英

How to Navigate to an instance of a UWP page?

I use below way 1 to Navigate to a page called PageTemplate (maybe from Page A), and initialize it with data1 .

this.Frame.Navigate(typeof(PageTemplate), data1);

And in PageTemplate , I use below way 2 to initialize its page data.

protected override async void OnNavigatedTo(NavigationEventArgs e)
{
    if (e.Parameter != null)
    {
        var data = e.Parameter as DataModel;
        ...
    }
}

And next time I may navigate from page B to PageTemplate and use data2 to initialize PageTemplate :

this.Frame.Navigate(typeof(PageTemplate), data2);

Question 1: So finally, PageTemplate with data1 and PageTemplate with data2 are both in navigation cache?

If not, how to achieve it? I mean a same page template (page elements and architect are same) with different data in navigation cache simoutinoeously.

Question 2: If we have solution to put same PageTemplate with data1/data2 as two different pages in navigation cache, how to navigate to them then?

So finally, PageTemplate with data1 and PageTemplate with data2 are both in navigation cache?

If we navigate with this order(A-->PageTemplate-->B-->PageTemplate), We could make sure PageTemplate with data1 and PageTemplate with data2 are both in navigation stack? And you could get above stack with BackStack property

If we have solution to put same PageTemplate with data1/data2 as two different pages in navigation cache, how to navigate to them then?

If you want to process the data1 and data2 type with PageTemplate at same time. we need set data1 with the source page's tag and detect the data type in PageTemplate then switch to different workflow.

Package the parameter with Dictionary that could store the source page tag into key part.

var parameter = new Dictionary<string, object>();
parameter.Add(nameof(MainPage), data1);
this.Frame.Navigate(typeof(PageTemplate), parameter);

Data Process

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    if (e.Parameter != null)
    {
        var data = e.Parameter as Dictionary<string, object>;
        foreach (var key in data.Keys)
        {
            switch (key)
            {
                case "MainPage":

                    break;
                case "OtherPage":

                    break;
                default:
                    break;
            }
        }
    }

    base.OnNavigatedTo(e);
}

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