0

我正在使用此代码让用户选择保存文件的位置:

        var fileSavePicker = new FileSavePicker();
        fileSavePicker.FileTypeChoices.Add("Pdf", new List<string>(){".pdf"});
        fileSavePicker.SuggestedFileName = $"{pdfFile.Name}";
        fileSavePicker.SuggestedSaveFile = pdfFile;
        fileSavePicker.PickSaveFileAndContinue();

此代码在 Windows Phone 8.1 上运行良好,但在 Windows 10 移动版上运行时出现异常 (System.UnauthorizedAccessException)。我该如何解决这个问题?

4

1 回答 1

0

当我在视觉对象中使用FileSavePicker.PickSaveFileAndContinue方法时,出现错误“ The FileSavePicker.PickSaveFileAndContinue() is obsolete: instead, use PickSaveFileAsync() ”。因此,您可以在 Windows 10 移动版中使用FileSavePicker.PickSaveFileAsync()方法。

在此处输入图像描述

更新: 我在 Windows 10 Mobile 上测试了 Windows Phone 8.1,没问题。下面是我项目的代码,大家可以参考。

您也可以参考这个关于FileSavePicker的示例。

 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.";
        }
    }
于 2016-12-27T09:33:27.317 回答