Windows Phone 7 Back Key Press event change? - windows-phone-7

I want to do some work if user presses back key on the phone with navigating back How to do it?
With following code it cancel navigation to back page & do some stuff but i want to do stuff while going back please help...
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
// do some stuff...
e.Cancel = true;
}

Related

How to enable PhoneApplicationPage_BackKeyPress event when set textbox.Focus() on textbox_LostFocus event in windows phone 8

How to enable
PhoneApplicationPage_BackKeyPress
event when set textbox.Focus() on textbox_LostFocus event in windows phone 8.
I want to show keyboard when i add some item into listbox from textbox. Listbox gets update but my keyboard always gets hide on button click.
I want to make it open all the time till i press back key.
Thanks,
Nishant
you can get BackKeyPress Event by :
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
base.OnBackKeyPress(e);
}
You can write textbox.Focus() at the end of button click when all work is done
try this :
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
ListboxName.Items.Add(TextBoxName.Text);
TextBoxName.Text = "";
TextBoxName.Focus();
}
I have implemented. Its working...

Windows Phone back button to terminate the app

I have a main page with some options. One of them is to navigate to page 1 where there are two arrows. One that navigates to page2, page3, and the other arrow page3, page2, page1 like a loop. There is also an arrow that navigates to main page.
I want to ask if there is a way when the user presses the back button to terminate the app from whatever page the user is currently at and not to navigate through all pages.
edit
if i want when i press the back to always navigate to the mainpage what i have to do ?
clear the back stack inside the onbackkeypress function where you want to exit the app. And it will exit the app normally.
[Updated]
1) after clearing back stack. Use NavigationService.Navigate(new Uri("MainPage.xaml",UriKind.Relative)); to traverse to mainpage and do e.Cancel = true in the next statement.
2) Or clear the back stack upto the mainpage. and automatically the back press will take you to the mainpage. inside the mainpage clear the back stack fully inside the OnNavigatedTo function so that the first item is always your mainpage and user can exit easily.
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
while (NavigationService.CanGoBack)
NavigationService.RemoveBackEntry();
e.Cancel = true;
return;
}
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
while (NavigationService.CanGoBack)
NavigationService.RemoveBackEntry();
}
Throw an exception in OnBackKeyPress which terminates the app.
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
throw an exception();
}
You can also do like this,
check
e.NavigationMode == System.Windows.Navigation.NavigationMode.Back
in OnNavigatedTo event (you need to override this every page) and call
NavigationService.GoBack();
there is no direct way of exiting an app.. see here
hope this will work. If its XNA
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

programmatically closing application on back key pressed event WP7

I'm trying to close my Wp7 app when the back is pressed in emulator.I have tried the code on that page from which I would close the whole app.the code is given below:
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
base.OnBackKeyPress(e);
if (NavigationService.CanGoBack)
{
while (NavigationService.RemoveBackEntry() != null)
{
NavigationService.RemoveBackEntry();
}
}
}
Help needed !
Thanks in advance!
First of all, There is no way to close wp7 application programatically.
And to do this you need not override back key press. When in the first page(ideally the home page or main page), pressing back key automatically closes the application.

prompt confirmation dialog when exit app

I am working on an app on wp7.
I hope to prompt a confirmation dialog when user exit app (press back button).
Is it possible?
Welcome any comment
Please handle the BackKeyPress button in the Application page to handle the back key press.
In Page.xaml file in the element add this code
BackKeyPress="PhoneApplicationPage_BackKeyPress"
it should look like
<phone:PhoneApplicationPage BackKeyPress="PhoneApplicationPage_BackKeyPress"
..//other attributes .. >
in event handler you write the code as follows
private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
{
MessageBoxResult mb = MessageBox.Show("You want exit the page", "Alert", MessageBoxButton.OKCancel);
if( mb != MessageBoxResult.OK)
{
e.Cancel = true;
}
}
It's possible to catch when the user exits pressing the Back button, but it is not possible to stop the application from being made "dormant" when the user presses the hardware Start button or Search buttons.
You can stop back navigation by set e.Cancel in back key press event.
In MainPage.xaml.cs constructor:
OnBackKeyPress += (s, e) =>
{
if (MessageBox.Show("", "", MessageBoxButtons.OkCancel) == MessageBoxButtons.Cancel)
{
e.Cancel = true;
};
};

How to handle back button in windows phone 7?

I have an application, the main page contains few functions. To explain in detail - I have save, color palette button in my main page. When any of these buttons are clicked, save pop up or color palette appears. How to handle back button of the device, when color palette or save pop up is opened. When back button is pressed at these scenario it should just make them invisible and stay on the main page. When nothing is being performed in the main page, then it should come out of the app. I tried to make their visibility collapsed on back button press. But it still is coming out of the application.
Please, guide me in this. Thanks in advance.
Override PhoneApplicationPage.OnBackKeyPress and then set CancelEventArgs.Cancel to true if you want to stop it from actually going back.
protected override void OnBackKeyPress(CancelEventArgs args)
{
if (PanelIsShowing)
{
HidePanel();
args.Cancel = true;
}
}
The back button behaves as is intended by Microsoft.
If you change its behavior you risk your application not being certified for the Marketplace.
If you want the back button to close the popups, turn the popups into pages so the back button navigates back to the main page..
You need to use
protected override void OnBackKeyPress(CancelEventArgs e)
{
if (_popup.IsOpen)
{
_popup.IsOpen= false;
e.Cancel = true;
}
else
{
base.OnBackKeyPress(e);
}
}
That should do the trick.

Resources