ContentDialog disappears when pressing Escape

Viewed 255

So I´m using: public sealed partial class SynchronizationDialog : ContentDialog.

I have an UWP app. When I press on a button in the UWP app the SynchronizationDialog opens and the app starts to download documents. The progress can be seen in the Dialog.

After the downloads are done the dialog "closes" by using this.Hide();

Now my problem is that when I press the Escape-key the Dialog disappears. Yet the download is still going and everything works just fine.

How can I prevent the Dialog from disappearing? I tried this:

public SynchronizationDialog()
{
    this.InitializeComponent();
    this.Loaded += SynchronizationDialog_Loaded;

    this.Closing += ContentDialog_Closing; //this is what I tried
}

//....

void ContentDialog_Closing(ContentDialog sender, ContentDialogClosingEventArgs args)
{
    bool doNotClose = true;
    if (doNotClose)
    {
        args.Cancel = true;
    }
}

The Dialog doesnt disappear this way. But after the download is finished and this.Hide(); happens the Dialog does not hide. I also cannot cancel the dialog so it stays there till I restart the app.

2 Answers

I would create a new boolean flag that would indicate if synchronization is still in progress and a custom method that will mark it as false and then proceed with Hide:

private bool _workInProgress = true;

public void FinishWork()
{
   _workInProgress = false;
   this.Hide();
}

Now the Closing event handler would be updated to the following:

void ContentDialog_Closing(ContentDialog sender, ContentDialogClosingEventArgs args)
{
    args.Cancel = _workInProgress;     
}

Now instead of Hide use the FinishWork method when the synchronization is complete.

Use a AppBarButton not a Button to solve the esc. exception error issue. The Dialog just closes when hitting the esc. key.

Related