Windows runtime 选择保存文件并继续:Windows 10 mobile上的UnauthorizedAccessException
我使用此代码让用户选择保存文件的位置:Windows runtime 选择保存文件并继续:Windows 10 mobile上的UnauthorizedAccessException,windows-runtime,windows-phone-8.1,windows-dev-center,Windows Runtime,Windows Phone 8.1,Windows Dev Center,我使用此代码让用户选择保存文件的位置: var fileSavePicker = new FileSavePicker(); fileSavePicker.FileTypeChoices.Add("Pdf", new List<string>(){".pdf"}); fileSavePicker.SuggestedFileName = $"{pdfFile.Name}"; fileSavePicker.Suggested
var fileSavePicker = new FileSavePicker();
fileSavePicker.FileTypeChoices.Add("Pdf", new List<string>(){".pdf"});
fileSavePicker.SuggestedFileName = $"{pdfFile.Name}";
fileSavePicker.SuggestedSaveFile = pdfFile;
fileSavePicker.PickSaveFileAndContinue();
var fileSavePicker=new fileSavePicker();
fileSavePicker.FileTypeChoices.Add(“Pdf”,newlist(){.Pdf});
fileSavePicker.SuggestedFileName=$“{pdfFile.Name}”;
fileSavePicker.SuggestedSaveFile=pdfFile;
fileSavePicker.PickSaveFileAndContinue();
此代码在Windows Phone 8.1上运行正常,但在Windows 10 mobile上运行时会出现异常(System.UnauthorizedAccessException)。如何解决此问题?当我在visual Studio中使用FileSavePicker.PickSaveFileAndContinue方法时,出现一个错误“FileSavePicker.PickSaveFileAndContinue()已过时:改为使用PickSaveFileAsync()”,这样您就可以在Windows 10 mobile中使用该方法 更新: 我已经在Windows10手机上测试了WindowsPhone8.1,还可以。下面是我的项目代码,你可以参考 您还可以参考此示例了解
private void savefile按钮\u单击(对象发送方,路由目标)
{
//在此场景的迭代之间,清除以前返回的文件名(如果存在)
OutputExtBlock.Text=“”;
FileSavePicker savePicker=新FileSavePicker();
savePicker.SuggestedStartLocation=PickerLocationId.DocumentsLibrary;
//用户可以将文件保存为的文件类型下拉列表
savePicker.FileTypeChoices.Add(“纯文本”,新列表(){.txt});
//如果用户未键入文件名或未选择要替换的文件,则为默认文件名
savePicker.SuggestedFileName=“新建文档”;
savePicker.PickSaveFileAndContinue();
}
///
///处理从文件选择器返回的文件
///此方法由基于ActivationKind的ContinuationManager触发
///
///文件保存选择器继续激活参数。它包含用户使用文件保存选择器选择的文件
公共异步void ContinueFileSavePicker(FileSavePickerContinuationEventArgs args args)
{
StorageFile=args.file;
如果(文件!=null)
{
//在完成更改并调用CompleteUpdatesAsync之前,禁止更新文件的远程版本。
CachedFileManager.DeferUpdates(文件);
//写入文件
等待FileIO.WriteTextAsync(file,file.Name);
//让Windows知道我们已完成更改文件,以便其他应用程序可以更新文件的远程版本。
//完成更新可能需要Windows请求用户输入。
FileUpdateStatus status=await CachedFileManager.CompleteUpdatesAsync(文件);
if(status==FileUpdateStatus.Complete)
{
OutputExtBlock.Text=“文件”+文件名+”已保存。“;
}
其他的
{
OutputExtBlock.Text=“文件”+文件名+”无法保存。“;
}
}
其他的
{
OutputExtBlock.Text=“操作已取消。”;
}
}
我在Windows 10 Mobile上测试了我的Windows Phone 8.1项目,一切正常。您可以参考我的更新以获取更多信息。谢谢,我认为问题在于这一行中的Pdfile是一个存储文件:fileSavePicker.SuggestedSaveFile=Pdfile事实上,在你的例子中,除了这个,你做了同样的事情。
private void SaveFileButton_Click(object sender, RoutedEventArgs e)
{
// Clear previous returned file name, if it exists, between iterations of this scenario
OutputTextBlock.Text = "";
FileSavePicker savePicker = new FileSavePicker();
savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
// Dropdown of file types the user can save the file as
savePicker.FileTypeChoices.Add("Plain Text", new List<string>() { ".txt" });
// Default file name if the user does not type one in or select a file to replace
savePicker.SuggestedFileName = "New Document";
savePicker.PickSaveFileAndContinue();
}
/// <summary>
/// Handle the returned file from file picker
/// This method is triggered by ContinuationManager based on ActivationKind
/// </summary>
/// <param name="args">File save picker continuation activation argment. It cantains the file user selected with file save picker </param>
public async void ContinueFileSavePicker(FileSavePickerContinuationEventArgs args)
{
StorageFile file = args.File;
if (file != null)
{
// Prevent updates to the remote version of the file until we finish making changes and call CompleteUpdatesAsync.
CachedFileManager.DeferUpdates(file);
// write to file
await FileIO.WriteTextAsync(file, file.Name);
// Let Windows know that we're finished changing the file so the other app can update the remote version of the file.
// Completing updates may require Windows to ask for user input.
FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
if (status == FileUpdateStatus.Complete)
{
OutputTextBlock.Text = "File " + file.Name + " was saved.";
}
else
{
OutputTextBlock.Text = "File " + file.Name + " couldn't be saved.";
}
}
else
{
OutputTextBlock.Text = "Operation cancelled.";
}
}