Aracılığıyla paylaş


How to continue your Windows Phone app after calling a file picker (HTML)

[This article is for Windows 8.x and Windows Phone 8.x developers writing Windows Runtime apps. If you’re developing for Windows 10, see the latest documentation]

When you call a file picker from a Windows Phone Store app, your app is deactivated until the file picker returns the selection made by the user. On low-memory phones, however, your app may be terminated. Because of this possibility, you have to call different methods in a Windows Phone Store app than you call in a Windows Store app to continue your app after a file picker operation. The following table shows these methods.

Task Method to call from a Windows Store app Method to call from a Windows Phone Store app
Pick a file to open PickSingleFileAsync PickSingleFileAndContinue
Pick a location and filename to save a file PickSaveFileAsync PickSaveFileAndContinue
Pick a folder PickSingleFolderAsync PickFolderAndContinue

 

The example in this topic demonstrates how to continue your app when you use a FileOpenPicker. Use similar code when you call other file and folder picker methods.

Tip  To see an example of this solution, see the File picker sample.

 

Instructions

Step 1: To call a FileOpenPicker and continue your app

The following example assumes that the user is selecting a photo by using a FileOpenPicker.

  1. Call the PickSingleFileAndContinue method to pick a photo by using a file picker.

        function pickSinglePhoto() {
            // Clean scenario output
            WinJS.log && WinJS.log("", "sample", "status");
    
            // Create the picker object and set options
            var openPicker = new Windows.Storage.Pickers.FileOpenPicker();
            openPicker.viewMode = Windows.Storage.Pickers.PickerViewMode.thumbnail;
            openPicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.picturesLibrary;
            // Users expect to have a filtered view of their folders depending on the scenario.
            // For example, when choosing a documents folder, restrict the filetypes to documents for your application.
            openPicker.fileTypeFilter.replaceAll([".png", ".jpg", ".jpeg"]);
    
            // Open the picker for the user to pick a file
            openPicker.pickSingleFileAndContinue();
        }
    
  2. Write a continuation method to take the desired action with the photo picked by the user when the app continues.

        // Called when app is activated from file open picker
        // eventObject contains the returned files picked by user
        function continueFileOpenPicker(eventObject) {
            var files = eventObject[0].files;
            var filePicked = files.size > 0 ? files[0] : null;
            if (filePicked !== null) {
                // Application now has read/write access to the picked file
                WinJS.log && WinJS.log("Picked photo: " + filePicked.name, "sample", "status");
            } else {
                // The picker was dismissed with no selected file
                WinJS.log && WinJS.log("Operation cancelled.", "sample", "status");
            }
        }
    
  3. Listen for the activated event.

        ...
        app.addEventListener("activated", activated, false);
        app.start();
    
  4. When the app is activated, handle the activated event to capture information about the activation and forward it to the page that called the file picker.

        function activated(eventObject) {
            var activationKind = eventObject.detail.kind;
            var activatedEventArgs = eventObject.detail.detail;
    
            // Handle launch and continuation activation kinds
            switch (activationKind) {
                case activationKinds.launch:
                case activationKinds.pickFileContinuation:
                case activationKinds.pickSaveFileContinuation:
                case activationKinds.pickFolderContinuation:
                case activationKinds.webAuthenticationBrokerContinuation:
                    var p = WinJS.UI.processAll().
                        then(function () {
    
                            // Navigate to either the first scenario or to the last running scenario
                            // before suspension or termination.
                            var url = "/pages/home/home.html";
                            var initialState = {};
                            var navHistory = app.sessionState.navigationHistory;
                            if (navHistory) {
                                nav.history = navHistory;
                                url = navHistory.current.location;
                                initialState = navHistory.current.state || initialState;
                            }
                            initialState.activationKind = activationKind;
                            initialState.activatedEventArgs = activatedEventArgs;
                            nav.history.current.initialPlaceholder = true;
                            return nav.navigate(url, initialState);
                        });
    
                    ...
                    break;
    
                default:
                    break;
            }
    
  5. When the user navigates to the page, check the ActivationKind property. If its value is pickFileContinuation, call the continuation method.

                if (options && options.activationKind === Windows.ApplicationModel.Activation.ActivationKind.pickFileContinuation) {
                    continueFileOpenPicker(options.activatedEventArgs);
    

File picker sample