【问题标题】:UnauthorizedAccessException when using FileOpenPicker使用 FileOpenPicker 时出现 UnauthorizedAccessException
【发布时间】:2014-12-17 09:05:15
【问题描述】:

我的 Windows 应用商店应用中有一个CommandBar,当我单击CommandBar 上的Open 按钮时,它会运行OpenFile 处理程序,如下所示:

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);
}

我很好地看到了消息,但只有当我点击Yes 时,我才会看到FileOpenPicker。当我点击No 时,我在以下行得到UnauthorizedAccessException: Access is denied.StorageFile file = await fileOpenPicker.PickSingleFileAsync();

我很困惑...有人知道为什么会这样吗?我什至尝试在调度程序中运行它,以防处理程序在不同的线程上被调用,但是......不幸的是,同样的事情:

await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, async () =>
{
    StorageFile file = await fileOpenPicker.PickSingleFileAsync();
    await LoadFile(file);
});

【问题讨论】:

  • 我认为这可能是由于一个愚蠢的 RT 竞争条件,它认为已经存在另一个对话框(即使没有)并且不会启动一个新的对话框(文件选择器),直到旧的(提示)已被解雇,并将尝试此处的一些技术并回发解决方案:stackoverflow.com/questions/12722490/…

标签: c# .net windows windows-store-apps windows-store


【解决方案1】:

是的,这是由于 RT 的对话竞争条件造成的。解决方案是让我字面上使用MessageDialog 类,就像在WinForms 中使用MessageBox.Show 一样:

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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-21
    • 1970-01-01
    • 1970-01-01
    • 2012-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多