简体   繁体   中英

Handling back button, changing pages in WP7

I have "MainPage" and "FriendsPage".How I can simulate the back button if user clicked the back button in "FrinedPage".MaiPage can show only one time.

MainPage>Friendspage, no returning back.

I think I need to use "OnNavigatedFrom" method, but I don't know how.Please help me.

On a button click call

NavigationService.GoBack();

or, in VB.NET,

NavigationService.GoBack()

Here is the MSDN docs: http://msdn.microsoft.com/en-us/library/system.windows.navigation.navigationservice.goback.aspx

You should be able to handle/Override OnBackKeyPress and set cancel = true to prevent moving backwards in your app.

Unlike WPF, one is not allowed to drop pages from the backstack in a Windows Phone app. Also there is certification requirement that from the first page of the app (assuming MainPage.xaml), the user should be able to hit "Back" & exit the app. So, please consider carefully what you are trying do.

That said, there are couple of ways:

  • Override & eat up OnBackKeyPress: This might lead to a jarring user experience though.

  • Override OnNavigatedTo() in MainPage and throw an unhandled exception to exit app or take other appropriate action (NavigationService.GoBack() or some other page). There was a nice post about exiting a SL app ( here ). Also, if using GoBack(), keep in mind that you are now taking over some responsibility to maintain the backstack after the jump; might have to be done consistently throughout the app.

Hope this helps!

There's probably a way to get what you want without violating the requirements.

what is on your "mainpage" that you don't want the user to go back to? from your limited info, it sounds like the mainpage shouldn't be your "main" page then.

You probably want your friendspage to be the main page, and have a popup or something that includes whatever you would have had on your "mainpage".

for other non-standard situations, there are some exceptions to the navigation rules, see the "nonlinear navigation" stuff: http://windowsteamblog.com/windows_phone/b/wpdev/archive/2010/12/13/solving-circular-navigation-in-windows-phone-silverlight-applications.aspx

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