2014-10-22 65 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); 
} 
相關問題