Windows Phone, pick file using PickSingleFileAndContinue or PickMultipleFilesAndContinue

前端 未结 1 386
陌清茗
陌清茗 2020-12-18 04:15

I got stuck trying to implementing file picker for windows phone app. I need to choose files from gallery using FileOpenPicker. I didn\'t get how it works. Here

1条回答
  •  既然无缘
    2020-12-18 05:11

    PickAndContinue is the only method that would work on Windows Phone 8.1. It's not so weird and ugly, here goes a simple example without ContinuationManager:

    Let's assume that you want to pick 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();
    

    Once you run PickSingleFileAndContinue();, your app is deactivated. When you finish picking a file, then OnActivated event is fired, where you can read the file(s) you have picked:

    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.
    

    Note that when you run file picker, your app is deactivated and in some rare situations it can be terminated by OS (little resources for example).

    The ContinuationManager is only a helper that should help to make some things easier. Of course, you can implement your own behaviour for simpler cases.

    0 讨论(0)
提交回复
热议问题