0

I would like to prevent changing the current page by pressing Back button or any navigation events while awaiting not finished. Because then exception happens, its should shown in the same page, on the other case it would be difficult to understand what action generate this exception

    private async void AppBarButton_Click(object sender, RoutedEventArgs e)
    {
        MessageDialog dialog = null;

        try
        {
            progressRing.IsActive = true;
            this.IsEnabled = false;
            commandBar1.IsEnabled = false;
            await GlobalVars.API.Call(CC.ChangeDate, date);
            var notify = new MessageDialog("Done");
            await notify.ShowAsync();
        }
        catch (Exception ex)
        {
            dialog = new MessageDialog(ex.Message);
        }
        finally
        {
            progressRing.IsActive = false;
            this.IsEnabled = true;
            commandBar1.IsEnabled = true;
        }
        if (dialog != null) await dialog.ShowAsync();  // must be shown in the same page
    }
Miles
  • 488
  • 5
  • 19
  • I'm not sure, that just disabling hardware buttons is a good practice. If the user wants to either move to the previous page or close the app it should be possible. I hope by "suppress navigation" you mean at least "handle navigation and provide the user with the choice to either cancel background operation and leave the page or wait for completion". – OpenMinded Nov 25 '14 at 13:56
  • 1
    If you use *NavigationHelper* then I think [this answer will help you](http://stackoverflow.com/a/27008026/2681948), otherwise you can subscribe to *HardwareButtons.BackPressed* event and implement your own behaviour. – Romasz Nov 25 '14 at 14:44
  • thanks, its that im looking for concerning NavigationHelper – Miles Nov 26 '14 at 13:26

1 Answers1

1

You can intercept the back button using:

protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
     e.Cancel = true;
}

Obviously you can do whatever you want in there.

James
  • 802
  • 5
  • 17
  • Is this approved when you submit your app to the store? – Benjamin Diele Nov 25 '14 at 13:28
  • Yes, have used in live, approved apps without issue. – James Nov 25 '14 at 13:34
  • You post title is "Suppress navigation while awaiting result on Windows Phone 8.1"? – James Nov 25 '14 at 13:44
  • yes, its my title. But i cant understand what you trying to tell me. Let me rephrase myself. I cant use you code in 8.1 project, or i'm wrong? – Miles Nov 25 '14 at 14:04
  • Works fine in a Windows Phone 8.1 Silverlight project (aka 'normal' Windows Phone). Not tested in a Windows Phone 8.1 Universal/RT project. Why don't you try it out? – James Nov 25 '14 at 14:28
  • my project is a simple Windows Phone 8.1 and not Universal/RT. I already try OnBackKeyPress but Page do not have this method – Miles Nov 25 '14 at 14:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/65612/discussion-between-miles-and-james-w). – Miles Nov 25 '14 at 15:05