ファイル ピッカーの呼び出し後に Windows Phone アプリを続行する方法 (HTML)

[ この記事は、Windows ランタイム アプリを作成する Windows 8.x および Windows Phone 8.x 開発者を対象としています。Windows 10 向けの開発を行っている場合は、「最新のドキュメント」をご覧ください]

Windows Phone ストア アプリからファイル ピッカーを呼び出すと、ユーザーによって選ばれたファイルが返されるまでの間、アプリは非アクティブ化された状態となります。ただし、メモリが少ない電話では、アプリが終了する場合があります。このような可能性があることから、Windows Phone ストア アプリでは、ファイル ピッカー操作の後もアプリの実行状態を維持するために、Windows ストア アプリの場合とは異なるメソッドを呼び出す必要があります。これらのメソッドを次に表にまとめます。

タスク Windows ストア アプリから呼び出すメソッド Windows Phone ストア アプリから呼び出すメソッド
開くファイルを選ぶ PickSingleFileAsync PickSingleFileAndContinue
ファイルの保存先とファイル名を選ぶ PickSaveFileAsync PickSaveFileAndContinue
フォルダーを選ぶ PickSingleFolderAsync PickFolderAndContinue

 

このトピックでは、FileOpenPicker を使ったときにアプリの実行状態を維持する方法を例示しています。その他のファイル ピッカー メソッドやフォルダー ピッカー メソッドを呼び出す際も同様のコードを使ってください。

ヒント  このソリューションの例については、ファイル ピッカーのサンプルに関するページをご覧ください。

 

手順

ステップ 1: FileOpenPicker を呼び出してアプリを継続させるには

次の例は、ユーザーが FileOpenPicker を使って写真を選ぶ状況を想定しています。

  1. PickSingleFileAndContinue メソッドを呼び出し、ファイル ピッカーを使って写真を選びます。

        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. アプリの実行が続行されるときに、ユーザーが選んだ写真を使って必要なアクションを実行する継続メソッドを作成します。

        // 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. activated イベントをリッスンします。

        ...
        app.addEventListener("activated", activated, false);
        app.start();
    
  4. アプリがアクティブ化されるときに、activated イベントを処理してアクティブ化に関する情報を取得し、ファイル ピッカーの呼び出し元となったページに転送します。

        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. ユーザーがページに移動したときに、ActivationKind プロパティを調べます。その値が pickFileContinuation であった場合は、継続メソッドを呼び出します。

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

関連トピック

ファイル ピッカーのサンプルに関するページ