Reputation: 9
I override BackButton in my app and show MessageDialog.
public Scenario_3()
{
this.InitializeComponent();
Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
}
private async void HardwareButtons_BackPresseed(object sender, BackPressedEventArgs e)
{
e.Handled = true;
var dialog = new MessageDialog("Do you want exit?");
dialog.Commands.Add(new Windows.UI.Popups.UICommand("yes") { Id = 0 });
dialog.Commands.Add(new Windows.UI.Popups.UICommand("no") { Id = 1 });
dialog.DefaultCommandIndex = 0;
dialog.CancelCommandIndex = 1;
var result = await dialog.ShowAsync();
if (result.Label == "yes")
{
this.Frame.Navigate(typeof(BlankPage1));
((Frame)Window.Current.Content).BackStack.Clear();
}
After navigating to BlankPage1 I again see MessageDialog if press on BackButton. How can I cancel this override after navigating?
Upvotes: 0
Views: 123
Reputation: 70671
Based on your response to my comment, it appears that you've simply forgotten to unsubscribe from the BackPressed
event when you navigate away from the page. Doing so will ensure that the event handler is called only when you want.
For example:
private async void HardwareButtons_BackPresseed(object sender, BackPressedEventArgs e)
{
e.Handled = true;
var dialog = new MessageDialog("Do you want exit?");
dialog.Commands.Add(new Windows.UI.Popups.UICommand("yes") { Id = 0 });
dialog.Commands.Add(new Windows.UI.Popups.UICommand("no") { Id = 1 });
dialog.DefaultCommandIndex = 0;
dialog.CancelCommandIndex = 1;
var result = await dialog.ShowAsync();
if (result.Label == "yes")
{
// Leaving this page. Stop listening for Back button presses.
Windows.Phone.UI.Input.HardwareButtons.BackPressed -= HardwareButtons_BackPressed;
this.Frame.Navigate(typeof(BlankPage1));
((Frame)Window.Current.Content).BackStack.Clear();
}
Upvotes: 1