我有一个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);
}
}
我怎样才能得到这个工作?
如果我的答案令人满意,请考虑关闭赏金。 – Mir
@Eve我以为我已经做到了。对于那个很抱歉。 – Arrow
完成。再次感谢:) – Arrow