简体   繁体   中英

Add page in Navigation stack in Windows Phone app

Hi I am building a Windows Phone 8.1 app and it is started with template blank Windows Phone app. In the app there are two pages 1. MainPage 2. DetailPage

Normal user flow is user clicks on tile -> MainPage -> DetailPage But now DetailPage can be pinned as secondary tile, so if secondary tile is clicked in App.xaml.cs OnLaunched I have written code like below:

Frame rootFrame = Window.Current.Content as Frame;

        if (rootFrame == null)
        {
            //Init rot frame and default logic of Windows Phone
        }

        if (rootFrame.Content == null)
        {
            // if (e.Arguments has indicate app launched from tile)
                //launch DetailPage
            //else
                //Launch MainPage
        }
        else
        {
            // if (e.Arguments has indicate app launched from tile)
                //launch DetailPage
            //else
                //Launch MainPage
        }
        // Ensure the current window is active
        Window.Current.Activate();

I understand the above code is crude, I am improving on it.

So now if user clicks hardware back button on DetailPage it comes to MainPage. For that I have used following code block.

if (Frame.CanGoBack) 
{
    e.Handled = true;
    Frame.GoBack(); 
}

So if user directy ends on DetailPage by clicking on secondary tile and then hits hardware back button I want it to come back on MainPage but it is directly exixting the app as MainPage is not present on BackStack. How do I achieve this?

I tried to use

Frame.BackStack.Add

method but could not understand how to make it work correctly.

Instead of going back using

Frame.GoBack();

simply navigate to the HomePage (and perhaps clean the BackStack)

Frame.Navigate(typeof(HomePage));
Frame.BackStack.Clear();

So, to put that in your context, something like this will work:

if (Frame.CanGoBack) 
{
    e.Handled = true;
    Frame.GoBack(); 
}
else
{
    // app was launched from a secondary tile
    e.Handled = true;
    Frame.Navigate(typeof(HomePage));
    Frame.BackStack.Clear();
}

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