首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用FileOpenPicker时的UnauthorizedAccessException

使用FileOpenPicker时的UnauthorizedAccessException
EN

Stack Overflow用户
提问于 2014-10-22 08:28:53
回答 1查看 168关注 0票数 1

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

代码语言:javascript
复制
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();

我是baffled...does有人知道为什么会这样吗?我甚至试着在调度程序中运行它,以防处理程序在另一个线程but...unfortunately上被调用,同样的事情:

代码语言:javascript
复制
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, async () =>
{
    StorageFile file = await fileOpenPicker.PickSingleFileAsync();
    await LoadFile(file);
});
EN

回答 1

Stack Overflow用户

发布于 2014-10-22 10:20:56

是的,这是由于RT的对话竞争条件。对我来说,解决方案是使用MessageDialog类,就像在WinForms中使用MessageBox.Show一样

代码语言:javascript
复制
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);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26498570

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档