私は Windows 8 の Metro スタイル アプリを使用しており、簡単に実行できるはずの作業に遭遇しましたが、解決策を見つけるのが非常に難しいと感じています。1 週間が経過しましたが、まだ解決策が見つからないため、有効な解決策として 300 担当者の報奨金を提供しています。
シナリオ:
テキスト ボックスを編集して [新しいドキュメントの作成] をクリックすると、新しいファイルを作成する前に既存のファイルに変更を保存するかどうかを尋ねるメッセージ ダイアログが表示されます。「はい、変更を保存します」をクリックすると、FileSavePicker が開き、ファイルを保存できます。
問題: [はい、変更を保存します] をクリックすると、ACCESSDENIED 例外が発生します。ブレークポイントを設定しましたが、例外の詳細から他の情報が明らかになりませんでした。
注:この場合は必須ではないため、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);
}
}
どうすればこれを機能させることができますか?