2013-01-05 48 views
3

單獨地,所有代碼都完美地工作。保存文件的片段,選擇保存目錄的片段以及消息對話框都非常有用。將MessageSavePicker與MessageDialog的IUICommand事件結合使用

但是當我將它們連接在一起時,我的訪問被拒絕。我沒有使用DocumentsLibrary功能,因爲在這種情況下不需要我這樣做,但是,在遇到問題後啓用此功能證實它不是問題。

場景: 用戶想要在文本框中輸入文本後創建一個新文檔。出現一個MessageDialog,詢問他們是否要先保存對現有文件的更改 - 用戶單擊是(保存文件)。

現在,這裏是您處理由MessageDialog引發的事件的地方。

在IUICommand命令事件處理程序中,您測試了哪個按鈕被單擊,並相應地執行。

我這樣做是與switch語句:

switch(command.Label) { 
    case "Yes": 
    SaveFile(); // extension method containing save file code that works on its own 
    break; 
    case "No": 
    ClearDocument(); 
    break; 
    default: 
    break; 
} 

現在,每一種情況下工作,除了是按鈕很大。當您單擊是時,將調用一個電子張力方法,其中的代碼保存到文件中

當您單擊yes按鈕時,您將收到ACCESS DENIED異常。例外的細節沒有透露任何內容。

我認爲這與我如何使用MesaageDialog有關。但是在搜索了幾個小時後,我還沒有找到一個關於如何使用FileSavePicker按下MesaageDialog按鈕時保存文件的示例。

任何想法應該怎麼做?

更新瓦特/代碼

當用戶點擊AppBar新建文件按鈕,此方法火災:

async private void New_Click(object sender, RoutedEventArgs e) 
{ 
    if (NoteHasChanged) 
    { 
     // Prompt to save changed before closing the file and creating a new one. 
     if (!HasEverBeenSaved) 
     { 

      MessageDialog dialog = new MessageDialog("Do you want to save this file before creating a new one?", 
       "Confirmation"); 
      dialog.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(this.CommandInvokedHandler))); 
      dialog.Commands.Add(new UICommand("No", new UICommandInvokedHandler(this.CommandInvokedHandler))); 
      dialog.Commands.Add(new UICommand("Cancel", new UICommandInvokedHandler(this.CommandInvokedHandler))); 

      dialog.DefaultCommandIndex = 0; 
      dialog.CancelCommandIndex = 2; 

      // Show it. 
      await dialog.ShowAsync(); 
     } 
     else { } 
    } 
    else 
    { 
     // Discard changes and create a new file. 
     RESET(); 
    } 
} 

而且FileSavePicker東西:

private void CommandInvokedHandler(IUICommand command) 
{ 
    // Display message showing the label of the command that was invoked 
    switch (command.Label) 
    { 
     case "Yes": 

      MainPage rootPage = this; 
      if (rootPage.EnsureUnsnapped()) 
      { 
       // Yes was chosen. Save the file. 
       SaveNewFileAs(); 
      } 
      break; 
     case "No": 
      RESET(); // Done. 
      break; 
     default: 
      // Not sure what to do, here. 
      break; 
    } 
} 

async public void SaveNewFileAs() 
{ 
    try 
    { 
     FileSavePicker saver = new FileSavePicker(); 
     saver.SuggestedStartLocation = PickerLocationId.Desktop; 
     saver.CommitButtonText = "Save"; 
     saver.DefaultFileExtension = ".txt"; 
     saver.FileTypeChoices.Add("Plain Text", new List<String>() { ".txt" }); 

     saver.SuggestedFileName = noteTitle.Text; 

     StorageFile file = await saver.PickSaveFileAsync(); 
     thisFile = file; 

     if (file != null) 
     { 
      CachedFileManager.DeferUpdates(thisFile); 

      await FileIO.WriteTextAsync(thisFile, theNote.Text); 

      FileUpdateStatus fus = await CachedFileManager.CompleteUpdatesAsync(thisFile); 
      //if (fus == FileUpdateStatus.Complete) 
      // value = true; 
      //else 
      // value = false; 

     } 
     else 
     { 
      // Operation cancelled. 
     } 

    } 
    catch (Exception exception) 
    { 
     Debug.WriteLine(exception.InnerException); 
    } 
} 
+0

您需要顯示SaveFile()方法。可能你無法訪問你的文件。嘗試選擇其他文件或通過嘗試和捕獲來捕獲該前端。 –

+0

@NorbertPisz:謝謝!現在我可以再次訪問我的電腦,現在我將發佈完整的代碼。 – Arrow

+0

@NorbertPisz用代碼更新。 :) – Arrow

回答

0

上的任何進步這個問題?我目前有同樣的問題。我還發現,如果在IUICommand事件中顯示第二個MessageDialog,則會發生同樣的問題。

我的解決方案是取消第一個操作(顯示第一個消息對話框)。這裏是一些代碼,我使用(它在一個全局對象訪問):

private IAsyncInfo mActiveDialogOperation = null; 
    private object mOperationMutex = new object(); 

    private void ClearActiveOperation(IAsyncInfo operation) 
    { 
     lock (mOperationMutex) 
     { 
      if (mActiveDialogOperation == operation) 
       mActiveDialogOperation = null; 
     } 
    } 

    private void SetActiveOperation(IAsyncInfo operation) 
    { 
     lock (mOperationMutex) 
     { 
      if (mActiveDialogOperation != null) 
      { 
       mActiveDialogOperation.Cancel(); 
      } 

      mActiveDialogOperation = operation; 
     } 
    } 

    public void StopActiveOperations() 
    { 
     SetActiveOperation(null); 
    } 

    public async void ShowDialog(MessageDialog dialog) 
    { 
     StopActiveOperations(); 

     try 
     { 
      IAsyncOperation<IUICommand> newOperation = dialog.ShowAsync(); 
      SetActiveOperation(newOperation); 
      await newOperation; 
      ClearActiveOperation(newOperation); 
     } 
     catch (System.Threading.Tasks.TaskCanceledException e) 
     { 
      System.Diagnostics.Debug.WriteLine(e.Message); 
     } 
    } 

所以每次我想顯示MessageDialog我打電話的ShowDialog。這將取消當前對話框(如果有的話)(然後發生TaskCanceledException)。

在我將使用FileSavePicker的情況下,我會在調用PickSaveFileAsync之前調用StopActiveOperations。

這個工程,但我不能說我喜歡它。這感覺就像我做錯了什麼。

0

好的,現在我已經明白了:-)。文檔說明確的,你不應該在UICommand顯示新的彈出窗口/文件選擇器:

http://msdn.microsoft.com/en-US/library/windows/apps/windows.ui.popups.messagedialog.showasync

這是一個不好的方式一個例子來做到這一點:

private async void Button_Click(object sender, RoutedEventArgs e) 
    { 
     MessageDialog dialog = new MessageDialog("Press ok to show new dialog (the application will crash)."); 
     dialog.Commands.Add(new UICommand("OK", new UICommandInvokedHandler(OnDialogOkTest1))); 
     dialog.Commands.Add(new UICommand("Cancel")); 

     await dialog.ShowAsync(); 
    } 

    private async void OnDialogOkTest1(IUICommand command) 
    { 
     MessageDialog secondDialog = new MessageDialog("This is the second dialog"); 
     secondDialog.Commands.Add(new UICommand("OK")); 

     await secondDialog.ShowAsync(); 
    } 

這是正確的方法:

private async void Button_Click_1(object sender, RoutedEventArgs e) 
    { 
     MessageDialog dialog = new MessageDialog("Press ok to show new dialog"); 
     UICommand okCommand = new UICommand("OK"); 
     UICommand cancelCommand = new UICommand("Cancel"); 

     dialog.Commands.Add(okCommand); 
     dialog.Commands.Add(cancelCommand); 

     IUICommand response = await dialog.ShowAsync(); 

     if(response == okCommand) 
     { 
      MessageDialog secondDialog = new MessageDialog("This is the second dialog"); 
      secondDialog.Commands.Add(new UICommand("OK")); 

      await secondDialog.ShowAsync(); 
     } 
    } 

其實很簡單,我應該早點得到...

相關問題