Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
222 views
in Technique[技术] by (71.8m points)

c# - Windows Phone, pick file using PickSingleFileAndContinue or PickMultipleFilesAndContinue

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 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 guess I need to get a file object or something.

I found this link. It is msdn explanation where custom class ContinuationManager is implemented. This solution looks weird and ugly. I am not sure if it is the best one. Please help!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...