2012-11-26 2 views
1

저는 Windows 8 Metro 스타일 앱을 보유하고 있으며 간단한 작업을 수행해야합니다.하지만 해결 방법을 찾는 것이 매우 어렵습니다. 지금 1 주일이 지났지 만 여전히 해결책을 찾지 못했지만, 나는 일하는 해결책을 위해 300 명의 대리점을 제공하고 있습니다.사용자가 MessageDialog에서 단추를 클릭 할 때 FileSavePicker를 호출하는 방법은 무엇입니까?

시나리오 :

내가 텍스트 상자를 편집하고 새 문서 만들기를 클릭

하는 MessageDialog 나는 새를 작성하기 전에 기존 파일에 변경 사항을 저장할지 묻는 나타납니다. "예, 변경 사항 저장"을 클릭하면 FileSavePicker가 열리고 파일을 저장할 수 있습니다.

문제 : "예, 변경 사항 저장"을 클릭하면 ACCESSDENIED 예외가 발생합니다. 중단 점을 설정했지만 예외 세부 사항은 다른 정보를 공개하지 않았습니다.

Notes :이 경우에는 요구 사항이 아니기 때문에 DocumentsLibrary 선언을 사용할 수 없으며 작동하지 않을 때 어쨌든 사용하도록 설정했기 때문에 오류가 발생했습니다.

또한 코드는 모두 완벽하게 작동하지만 (서로 분리되어 있음), 모두 묶어서 FileSavePicker를 열려고하면 분리됩니다.

저는 이것이 스레딩 문제 일 수 있다고 생각합니다. 그러나 나는 확실하지 않다.

MessageDialog (MSDN)

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

내 대답이 만족 스럽다면, 현상금 마감을 고려하십시오. – Mir

+0

@Eve 나는 이미 그렇게했다고 생각했습니다. 미안합니다. – Arrow

+0

완료. 다시 한 번 감사드립니다 :) – Arrow

답변

4

이 종료되기 전에 (그리고 await dialog.ShowAsync() 호출이 종료되기 전에) 문제가있는 것 같습니다. 이 문제가 발생하는 이유가 확실하지 않지만 해결 방법을 쉽게 수행 할 수 있습니다. 나는 단지 예를 든다. 당신의 필요와 모델에 적응시키는 것은 당신에게 달려있다.

먼저 Enum을 선언하십시오.

enum SaveChoice 
{ 
    Undefined, 
    Save, 
    DoNotSave 
} 

그런 다음 클래스에 필드/속성을 만듭니다. 다시 말하지만, 이것은 가장 현명한 디자인 선택은 아니지만 예제로 작동합니다. 메시지를 확인하기 위해 파일 선택기를 호출하기 전에

//Continues from dialog.CancelCommandIndex = 2; 
// Show it. 
await dialog.ShowAsync(); 
if (_currentChoice == SaveChoice.Save) SaveNewFileAs(); 
+0

감사합니다 @ 이브 - 멋진 일 :) – Arrow

+1

그 좋은 일! – Sakthivel

+0

실제로 그것은 권자입니다. – Arrow

1

당신은 작은 지연을 도입 할 수 있습니다 : 당신의 New_Click 방법을 수정, 마지막으로

void CommandInvokedHandler(IUICommand command) 
{ 
    // Display message showing the label of the command that was invoked 
    switch (command.Label) 
    { 
     case "Yes": 
      var rootPage = this; 
      if (rootPage.EnsureSnapped()) 
       _currentChoice = SaveChoice.Save; 
      break; 
     case "No": 
      _currentChoice = SaveChoice.DoNotSave; 
      break; 
     default: 
      _currentChoice = SaveChoice.Undefined; 
      // Not sure what to do, here. 
      break; 
    } 
} 

:

SaveChoice _currentChoice; 

그런 다음 CommandInvokeHandler 방법을 수정 대화 상자가 종료되었습니다.

await Task.Delay(10); 
StorageFile file = await saver.PickSaveFileAsync(); 
관련 문제