1
时,我有一个CommandBar
在我的Windows应用商店的应用程序,当我点击我的CommandBar
的Open
按钮,它如下运行OpenFile
处理程序:UnauthorizedAccessException使用FileOpenPicker
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竞争条件对话框存在(即使没有),并且不会启动一个新的(文件选取器),直到旧的(提示符)被解除,并且会尝试这里的一些技巧并回传一个解决方案:http:/ /stackoverflow.com/questions/12722490/messagedialog-showasync-throws-accessdenied-exception-on-second-dialog – Alexandru 2014-10-22 01:08:08