Windows Phone, select file using PickSingleFileAndContinue or PickMultipleFilesAndContinue

I am stuck trying to implement a file collector for a Windows Phone application. I need to select files from the gallery using FileOpenPicker . I did not understand how this works. Here is my code:

 private readonly FileOpenPicker photoPicker = new FileOpenPicker(); // This is a constructor public MainPage() { // < ... > photoPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; photoPicker.FileTypeFilter.Add(".jpg"); } // I have button on the UI. On click, app shows picker where I can choose a file private void bChoosePhoto_OnClick(object sender, RoutedEventArgs e) { photoPicker.PickMultipleFilesAndContinue(); } 

So what to do next? I think I need to get a file object or something else.

I found this link . This is an explanation of msdn where the custom ContinuationManager class is implemented. This decision looks strange and ugly. I am not sure if this is the best. Please, help!

+6
source share
1 answer

PickAndContinue is the only method that will work on Windows Phone 8.1. This is not so weird and ugly, here is a simple example without ContinuationManager:

Suppose you want to select a .jpg file, you use FileOpenPicker:

 FileOpenPicker picker = new FileOpenPicker(); picker.FileTypeFilter.Add(".jpg"); picker.ContinuationData.Add("keyParameter", "Parameter"); // some data which you can pass picker.PickSingleFileAndContinue(); 

After running PickSingleFileAndContinue(); your application will be disconnected. When you finish collecting the file, the OnActivated event will be fired, where you can read the selected files:

 protected async override void OnActivated(IActivatedEventArgs args) { var continuationEventArgs = args as IContinuationActivatedEventArgs; if (continuationEventArgs != null) { switch (continuationEventArgs.Kind) { case ActivationKind.PickFileContinuation: FileOpenPickerContinuationEventArgs arguments = continuationEventArgs as FileOpenPickerContinuationEventArgs; string passedData = (string)arguments.ContinuationData["keyParameter"]; StorageFile file = arguments.Files.FirstOrDefault(); // your picked file // do what you want break; // rest of the code - other continuation, window activation etc. 

Please note that when you start the file picker, your application is turned off, and in some rare cases it can be stopped by the OS (for example, small resources).

ContinuationManager is just an assistant that should make things easier. Of course, you can implement your behavior for simpler cases.

+12
source

Source: https://habr.com/ru/post/973019/


All Articles