[英]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.