我是 Windows Phone 开发的新手,我必须使用 phonegap/cordova 将我的应用程序移植到它上面。我的大部分代码都适用于 Android/iOS 和 winphone,但在这个 FileOpenPicker 上我被阻止了。我正在使用 winjs 2.1,我想准备一个脚本,以便在我在需要此功能的页面中时调用。
我已经阅读了大量示例,并且我认为我非常接近解决方案。
在我的 html 文件中,我声明:
<script src="//Microsoft.Phone.WinJS.2.1/js/base.js"></script>
<script src="//Microsoft.Phone.WinJS.2.1/js/ui.js"></script>
<script type="text/javascript" src="js/default.js"></script>
这是我的 default.js,我在必须调用 FileOpenPicker 的页面中使用的文件。
(function () {
"use strict";
var app = WinJS.Application;
var activation = Windows.ApplicationModel.Activation;
app.onloaded = function (args) {
var activationKind = args.detail.kind;
document.getElementById("btnSnap").addEventListener("click", pickSinglePhoto);
if (activationKind === Windows.ApplicationModel.Activation.ActivationKind.pickFileContinuation) {
continueFileOpenPicker(options.activatedEventArgs);
}
};
function pickSinglePhoto() {
// Clean scenario output
WinJS.log && WinJS.log("", "sample", "status");
console.log("in pickSinglePhoto");
// 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) {
console.log("in continueFileOpenPicker");
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");
}
}
app.start();
})();
不幸的是,这不起作用。我无法进入 continueFileOpenPicker 因为标志 activationKind 始终未定义。我很确定我应该使用 app.onactivated 而不是 app.onloaded,但在前一种情况下,我无法进入该功能。
我已经尝试了 pickSinglePhoto 函数,它似乎可以工作,但由于应用程序崩溃,我无法在选择后返回页面,显然是因为我无法在其他 javascript 文件中选择和使用该函数。
有什么线索吗?