2012-11-26 53 views
1

我有一个Windows 8 Metro风格的应用程序,我遇到了应该是一件简单的事情来完成,但我觉得很难提出一个解决方案。由于现在已经有一周了,而且我还没有找到解决方案,所以我提供了300个代表处的解决方案。当用户点击MessageDialog上的按钮时如何调用FileSavePicker?

场景:

当我编辑一个文本框,然后单击创建新文档,一个MessageDialog出现,询问我是否创建一个新的人之前将更改保存到现有的文件。如果我点击“是,保存更改” - 那么FileSavePicker会打开,允许我保存文件。

问题: 当我点击“是,保存更改”,我得到一个ACCESSDENIED异常。我设置了断点,但异常详细信息没有显示任何其他信息。

备注: 我没有启用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

问题似乎是在MessageDialog关闭之前(和await dialog.ShowAsync()呼叫终止之前)您打开FileSavePicker。我不确定为什么会发生这种情况,但可以轻松完成解决方法。我只做一个例子,它将取决于你的需求和模型。首先申报Enum

enum SaveChoice 
{ 
    Undefined, 
    Save, 
    DoNotSave 
} 

然后,在你的类中创建一个字段/属性。再次,这不是最明智的设计选择,但它是一个例子。

SaveChoice _currentChoice; 

然后修改CommandInvokeHandler方法:

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; 
    } 
} 

最后,编辑您New_Click方法:

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

谢谢@Eve - 不错的工作:) – Arrow

+1

它的一个很好的解决方法! – Sakthivel

+0

事实上,它是大声笑。 – Arrow

1

您可以引入一个微小的延迟调用文件选择器,以确保该消息之前对话已结束。

await Task.Delay(10); 
StorageFile file = await saver.PickSaveFileAsync(); 
相关问题