1

I have a CommandBar in my Windows Store app, and when I click the Open button on my CommandBar, it runs the OpenFile handler as follows:

private async void OpenFile(object sender, RoutedEventArgs e)
{
    MessageDialog dialog = new MessageDialog("You are about to open a new file. Do you want to save your work first?");
    dialog.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(SaveAndOpen)));
    dialog.Commands.Add(new UICommand("No", new UICommandInvokedHandler(Open)));
    await dialog.ShowAsync();
}

private async void SaveAndOpen(IUICommand command)
{
    await SaveFile();
    Open(command);
}

private async void Open(IUICommand command)
{
    FileOpenPicker fileOpenPicker = new FileOpenPicker();
    fileOpenPicker.ViewMode = PickerViewMode.List;
    fileOpenPicker.FileTypeFilter.Add(".txt");
    StorageFile file = await fileOpenPicker.PickSingleFileAsync();
    await LoadFile(file);
}

I see the message just fine, but only when I hit Yes do I get presented with the FileOpenPicker. When I hit No I get an UnauthorizedAccessException: Access is denied. at the following line: StorageFile file = await fileOpenPicker.PickSingleFileAsync();

I'm baffled...does anyone know why this is happening? I even tried running it in a dispatcher on the off-chance that the handler was being called on a different thread, but...unfortunately, same thing:

await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, async () =>
{
    StorageFile file = await fileOpenPicker.PickSingleFileAsync();
    await LoadFile(file);
});
Alexandru
  • 12,264
  • 17
  • 113
  • 208
  • I think this may be due to a dumb RT race condition where it thinks there is already another dialog present (even though there isn't) and will not start a new one (the file picker) until the old (prompt one) is dismissed, and will try some of the techniques in here and post back a solution: http://stackoverflow.com/questions/12722490/messagedialog-showasync-throws-accessdenied-exception-on-second-dialog – Alexandru Oct 22 '14 at 01:08

1 Answers1

0

Yes, it was due to RT's dialog race conditions. The solution was for me to literally make use of the MessageDialog class the same way you would use MessageBox.Show in WinForms:

private async void OpenFile(object sender, RoutedEventArgs e)
{
    MessageDialog dialog = new MessageDialog("You are about to open a new file. Do you want to save your work first?");
    IUICommand result = null;
    dialog.Commands.Add(new UICommand("Yes", (x) =>
    {
        result = x;
    }));
    dialog.Commands.Add(new UICommand("No", (x) =>
    {
        result = x;
    }));
    await dialog.ShowAsync();
    if (result.Label == "Yes")
    {
        await SaveFile();
    }
    FileOpenPicker fileOpenPicker = new FileOpenPicker();
    fileOpenPicker.ViewMode = PickerViewMode.List;
    fileOpenPicker.FileTypeFilter.Add(".txt");
    StorageFile file = await fileOpenPicker.PickSingleFileAsync();
    await LoadFile(file);
}
Alexandru
  • 12,264
  • 17
  • 113
  • 208