[ 本文适用于编写 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 选择了一个新照片。
调用 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(); }当应用继续时编写延续方法,以对由用户选取的照片采取所需的操作。
// 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"); } }侦听 activated 事件。
... app.addEventListener("activated", activated, false); app.start();当激活应用时,处理 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; }当用户导航到该页面时,检查 ActivationKind 属性。如果其值为 pickFileContinuation,则调用延续方法。
if (options && options.activationKind === Windows.ApplicationModel.Activation.ActivationKind.pickFileContinuation) { continueFileOpenPicker(options.activatedEventArgs);