單獨地,所有代碼都完美地工作。保存文件的片段,選擇保存目錄的片段以及消息對話框都非常有用。將MessageSavePicker與MessageDialog的IUICommand事件結合使用
但是當我將它們連接在一起時,我的訪問被拒絕。我沒有使用DocumentsLibrary功能,因爲在這種情況下不需要我這樣做,但是,在遇到問題後啓用此功能證實它不是問題。
場景: 用戶想要在文本框中輸入文本後創建一個新文檔。出現一個MessageDialog
,詢問他們是否要先保存對現有文件的更改 - 用戶單擊是(保存文件)。
現在,這裏是您處理由MessageDialog
引發的事件的地方。
在IUICommand命令事件處理程序中,您測試了哪個按鈕被單擊,並相應地執行。
我這樣做是與switch語句:
switch(command.Label) {
case "Yes":
SaveFile(); // extension method containing save file code that works on its own
break;
case "No":
ClearDocument();
break;
default:
break;
}
現在,每一種情況下工作,除了是按鈕很大。當您單擊是時,將調用一個電子張力方法,其中的代碼保存到文件中
當您單擊yes按鈕時,您將收到ACCESS DENIED異常。例外的細節沒有透露任何內容。
我認爲這與我如何使用MesaageDialog
有關。但是在搜索了幾個小時後,我還沒有找到一個關於如何使用FileSavePicker
按下MesaageDialog
按鈕時保存文件的示例。
任何想法應該怎麼做?
更新瓦特/代碼
當用戶點擊AppBar新建文件按鈕,此方法火災:
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);
}
}
您需要顯示SaveFile()方法。可能你無法訪問你的文件。嘗試選擇其他文件或通過嘗試和捕獲來捕獲該前端。 –
@NorbertPisz:謝謝!現在我可以再次訪問我的電腦,現在我將發佈完整的代碼。 – Arrow
@NorbertPisz用代碼更新。 :) – Arrow