- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
就个人而言,所有代码都能完美运行。用于保存文件的代码片段、用于选择要将其保存到的目录的代码片段以及消息对话框都非常有效。
但是,当我将它们全部联系在一起时,访问被拒绝。我没有使用 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;
}
现在,除了"is"按钮外,每个案例都很好用。当您单击是时,将调用一个电子张力方法,该方法具有保存到文件的代码
当您单击"is"按钮时,您会收到“拒绝访问”异常。异常的详细信息没有透露任何信息。
我认为这与我使用 MesaageDialog
的方式有关。但是在搜索了几个小时之后,我还没有找到有关如何在按下 MesaageDialog
按钮时使用 FileSavePicker
保存文件的示例。
关于如何做到这一点有什么想法吗?
当用户单击 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);
}
}
最佳答案
这个问题有什么进展吗?我目前有同样的问题。我还发现,如果在 IUICommand
事件中显示第二个 MessageDialog
,也会出现同样的问题。
我的解决方案是取消第一个操作(显示第一个消息对话框)。这是我正在使用的一些代码(它可以在全局对象中访问):
private IAsyncInfo mActiveDialogOperation = null;
private object mOperationMutex = new object();
private void ClearActiveOperation(IAsyncInfo operation)
{
lock (mOperationMutex)
{
if (mActiveDialogOperation == operation)
mActiveDialogOperation = null;
}
}
private void SetActiveOperation(IAsyncInfo operation)
{
lock (mOperationMutex)
{
if (mActiveDialogOperation != null)
{
mActiveDialogOperation.Cancel();
}
mActiveDialogOperation = operation;
}
}
public void StopActiveOperations()
{
SetActiveOperation(null);
}
public async void ShowDialog(MessageDialog dialog)
{
StopActiveOperations();
try
{
IAsyncOperation<IUICommand> newOperation = dialog.ShowAsync();
SetActiveOperation(newOperation);
await newOperation;
ClearActiveOperation(newOperation);
}
catch (System.Threading.Tasks.TaskCanceledException e)
{
System.Diagnostics.Debug.WriteLine(e.Message);
}
}
因此,每次我想显示一个 MessageDialog 时,我都会调用 ShowDialog。这将取消当前对话框(如果有的话)(然后会发生 TaskCanceledException)。
如果我将使用 FileSavePicker,我会在调用 PickSaveFileAsync 之前调用 StopActiveOperations。
这很管用,但我不能说我喜欢它。感觉我做错了什么。
关于c# - 将 FileSavePicker 与 MessageDialog 的 IUICommand 事件一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14176469/
我有一个应用程序,用户可以在其中下载文件。我正在尝试使用 FileSavePicker让他选择位置和文件名。该文件可以是任何类型,所以我想要 FileSavePicker接受任何文件类型,但显然我不允
我很难弄清楚这个错误的原因。我在 Manifest 中添加了 FilePicker 功能,这并不是我想做任何疯狂的事情;只是试图保存到文档文件夹中的子文件夹... Error: "An unhandl
我正在尝试将现有文件保存到另一个地方。这是某种副本,但我想允许使用 FileSavePicker 的用户选择新的目的地。这是我的代码: StorageFile currentImage = await
我是第一次尝试使用 FileSavePicker,但是当我调用 await picker.PickSaveFileAsync(); 时,我不断收到“未指定的错误”,没有异常源。 我注意到异常数据字典包
努力使用文件保存选择器。我希望我的 savepicker 保存文件,我用 openpicker 打开并编辑它们。但是当我保存时,我通过 Savepicker 收到一个空的 0 字节文件。我可以使用本地
我有一个 Windows 8 Metro 风格的应用程序,我遇到了一个应该很容易完成的事情,但我发现很难找到一个解决方案。因为已经一周了,我仍然没有找到解决方案,所以我提供 300 代表的赏金来寻找可
用 var... 行更新了我的 SoundData 中的代码。我实际上已经看到要保存到现在的文件夹,但是当我尝试选择一个文件夹并保存到其中时出现异常。 + $exception {System.IO.
就个人而言,所有代码都能完美运行。用于保存文件的代码片段、用于选择要将其保存到的目录的代码片段以及消息对话框都非常有效。 但是,当我将它们全部联系在一起时,访问被拒绝。我没有使用 DocumentsL
我的第一个 WinUI 3 项目。我的第二个 C# 项目。我已经能够让 FileSavePicker 工作并显示一个对话框: 我要展示的是一个 stub 程序,它只是为了解决我在使用 FileSave
我想使用文件保存选择器保存图像。我正在使用 this链接保存,但它仅用于文本,我如何修改它以保存图像? 最佳答案 正如您所提供的 the link那么我假设您在 Continuation 之后设法获得
我想知道如何在 Metro 应用程序上更改 FileSavePicker 的主题。我只为 android 找到了这个问题的答案,但并没有太大帮助。而且我在 msdn 文档中没有找到任何内容。那么是否可
我正在尝试使用 FilePicker Task 保存视频文件,但它没有在解决方案中显示 FileSavePicker 任务。下面附上截图 Windows.Storage.Pickers 中只显示 4
我是一名优秀的程序员,十分优秀!