共用方式為


如何在呼叫檔案選擇器之後繼續執行您的 Windows Phone 應用程式 (HTML)

[ 本文的目標對象是撰寫 Windows 執行階段 App 的 Windows 8.x 和 Windows Phone 8.x 開發人員。如果您正在開發適用於 Windows 10 的 App,請參閱 最新文件 ]

當您從 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);
    

相關主題

檔案選擇器範例