我有一个 Windows 8 Metro 风格的应用程序,我遇到了一个应该很容易完成的事情,但我发现很难找到一个解决方案。因为已经一周了,我仍然没有找到解决方案,所以我提供 300 代表的赏金来寻找可行的解决方案。
场景:
当我编辑文本框并单击创建新文档时,会出现一个消息对话框,询问我是否要在创建新文件之前保存对现有文件的更改。如果我单击“是,保存更改”——然后 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);
}
}
我怎样才能让它工作?
最佳答案
问题似乎是您在 MessageDialog
关闭之前打开了一个 FileSavePicker
(并且在 await dialog.ShowAsync()
之前) > 调用终止)。我不确定为什么会发生这种行为,但可以轻松完成解决方法。我只会举一个例子,您可以根据自己的需求和模型对其进行调整。
首先,声明一个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();
关于c# - 用户单击 MessageDialog 上的按钮时如何调用 FileSavePicker?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13561760/