2014-10-22 42 views
1

时,我有一个CommandBar在我的Windows应用商店的应用程序,当我点击我的CommandBarOpen按钮,它如下运行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); 
}); 
+0

我想这可能是因为它认为已经有另一哑RT竞争条件对话框存在(即使没有),并且不会启动一个新的(文件选取器),直到旧的(提示符)被解除,并且会尝试这里的一些技巧并回传一个解决方案:http:/ /stackoverflow.com/questions/12722490/messagedialog-showasync-throws-accessdenied-exception-on-second-dialog – Alexandru 2014-10-22 01:08:08

回答

0

是,它RT的对话框竞争条件是由于。解决的办法是让我字面上利用MessageDialog类以同样的方式,你会用MessageBox.ShowWinForms

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); 
} 
相关问题