How to go to previous page in windows phone 8 when back button is pressed

血红的双手。 提交于 2019-12-13 06:54:48

问题


I have a home page or landing page in my windows phone c# based app where user enters login details and upon successful login user is redirected to page2 . Here the user will see a list box with few items . Upon selecting an item from this list box a new page called "Threadx" opens.(where x is the each page that opens upon clicking the x item in the list box)

While user is on this Thread page "Threadx" he may receive the toast notifications and the thread gets updated with new replies or answers on that thread.

But When user clicks on back button the "ThreadX" page doesn't get closed and instead it goes to its previous state where it has less number of messages , and so on until the app gets closed.

protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
        if (e.NavigationMode == NavigationMode.Back)
        {
            return;
        }
    }

I would like to know if this "Threadx" page can be closed upon clicking back button without affecting other "Threadx+1","Threadx+2"..."Threadx+n" pages.

Any help would be really appreciated.


回答1:


Normally windows keeps the pages on it's stack when you leave a page and navigate to another page. If you want to navigate to the previous page on pressing the Back Button you can do following things:

Add following line to OnNavigatedTo method:

Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;

Add definition for HardwareButtons_BackPressed method:

    private void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
    {
        e.Handled = true;
        if (Frame.CanGoBack)
            Frame.GoBack();
    }

Don't forget to add Windows.Phone.UI.Input to the namespace list.




回答2:


The other way I got it worked was using the below code in the onnavigateto method in my "thread" page and it worked for me. Let me know if there is an elegant way of doing it or better way of doing it .

if (e.NavigationMode == NavigationMode.Back)
         {
             NavigationService.Navigate(new Uri("/View/Page2.xaml", UriKind.Relative));
         }


来源:https://stackoverflow.com/questions/28966170/how-to-go-to-previous-page-in-windows-phone-8-when-back-button-is-pressed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!