我的Windows Store应用程序中有一个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();
我是baffled...does有人知道为什么会这样吗?我甚至试着在调度程序中运行它,以防处理程序在另一个线程but...unfortunately上被调用,同样的事情:
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, async () =>
{
StorageFile file = await fileOpenPicker.PickSingleFileAsync();
await LoadFile(file);
});发布于 2014-10-22 10:20:56
是的,这是由于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);
}https://stackoverflow.com/questions/26498570
复制相似问题