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