2016-12-23 13 views
0

en utilisant ce code pour permettre aux utilisateurs de choisir où enregistrer un fichier:PickSaveFileAndContinue: UnauthorizedAccessException sur Windows 10 Je suis mobile

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

Ce code fonctionne très bien sur Windows Phone 8.1, mais donnez-moi une exception (System.UnauthorizedAccessException) lors de l'exécution sur Windows 10 mobile. Comment puis-je resoudre ceci?

Répondre

0

Lorsque j'utilise la méthode FileSavePicker.PickSaveFileAndContinue dans le visuel, il y a une erreur « Le FileSavePicker.PickSaveFileAndContinue() est obsolète: utilisez plutôt PickSaveFileAsync() ». Ainsi, vous pouvez utiliser la méthode FileSavePicker.PickSaveFileAsync() dans Windows 10 mobile.

enter image description here

Mise à jour: Je test de Windows Phone 8.1 sur Windows 10 Mobile, il était ok. Le code de mon projet ci-dessous, vous pouvez vous référer à. Vous pouvez également vous référer à cet exemple sur 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."; 
     } 
    } 
+0

Je vois le contraire: [link] (https://postimg.org/image/htj36l3c1/) –

+0

Je teste mon projet Windows Phone 8.1 sur Windows 10 Mobile, il est ok. vous pouvez vous référer à ma mise à jour en réponse pour plus d'informations. –

+0

Merci, je pense que le problème était cette ligne dans laquelle pdfFile est un StorageFile: 'fileSavePicker.SuggestedSaveFile = pdfFile;' infact dans votre exemple, vous faites les mêmes choses, sauf cela. –