【发布时间】: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