Call Back in windows phone 7
I nee开发者_StackOverflow中文版d to call the back button command at some point in my application programmatically, so what is the command for that.
The issue is that i need the application to exit when the user presses back from the second page, hence i need to call the back button command when navigated on to the first page to exit. I searched online and found out that there is no systematic way to exit an application in code so any suggestion? Thanks,Call NavigationService.RemoveBackEntry();
in OnNavigatedTo
of the second page. This will remove the first page from the navigation stack and if the user presses the back key the application will exit.
Warning: Call remove only once! (multitasking calls OnNavigatedTo
multiple times)
private bool removed = false;
public override void OnNavigatedTo(...)
{
if (!removed)
{
removed = true;
NavigationService.RemoveBackEntry();
}
(WP 7.1 [Mango] only)
I found that this worked for me using Windows Phone SDK 7.1 (mango):
private bool removeBack = true;
private void PhoneApplicationPage_BackKeyPress ( object sender , System . ComponentModel . CancelEventArgs e )
{
if ( removeBack )
{
this . removeBack = false;
if ( NavigationService . CanGoBack )
{
this . NavigationService . RemoveBackEntry ( );
}
}
}
NavigationService.GoBack();
This can be called on a Page to go back again - if this is the first page your app will exit, you can't force an exit from an application - in 7.1 there are methods to remove pages from the Navigation "backstack" that can skip a page to achieve a similar effect such as:
NavigationService.RemoveBackEntry();
However that does only work on "Mango" SDK and devices/emulator
Sorry this solution doesn't work have checked this, but am leaving this here incase it is useful to someone.
精彩评论