![](/img/trans.png)
[英]How to call javascript method when user clicks on button(not on page_loadcomplete)?
[英]How to call FileSavePicker when user clicks button on MessageDialog?
我有一个Windows 8 Metro风格的应用程序,遇到了应该很简单的事情,但是发现解决方案非常困难。 由于已经有一个星期了,但是我仍然没有找到解决方案,因此我提供300 rep的赏金来解决这个问题。
场景:
当我编辑一个文本框并单击“创建新文档”时,会出现一个MessageDialog,询问我是否要在创建新文件之前将更改保存到现有文件中。 如果单击“是,保存更改”-然后FileSavePicker打开,允许我保存文件。
问题:当我点击“是的,保存更改”,我得到一个存取遭拒例外。 我已经设置了断点,但是异常详细信息没有显示任何其他信息。
注意:我没有启用DocumentsLibrary声明,因为在这种情况下这不是必需的,并且当此方法不起作用时,无论如何我都尝试启用它-仍然出现错误。
同样,所有代码段都可以完美地独立工作(彼此独立),但是当您将所有代码捆绑在一起时,当FileSavePicker尝试打开时,它们会崩溃。
我相信这可能是线程问题。 但是我不确定。
MSDN上的MessageDialog 。
我的代码如下:
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
之前(以及在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();
您可以在调用文件选择器之前引入一点延迟,以确保消息对话框已结束。
await Task.Delay(10);
StorageFile file = await saver.PickSaveFileAsync();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.