I'm new in windows phone development and I have to port my app on it, using phonegap/cordova. Most of my code works for Android/iOS and also winphone, but on this FileOpenPicker I'm blocked. I'm using winjs 2.1 and I'd like to prepare a script to be called when I'm in a page needing this functionality.
I've read a ton of examples, and I think I'm pretty near the solution.
In my html file I declare:
<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>
And This is my default.js, the file I use in the page where the FileOpenPicker have to be called.
(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();
})();
Unfortunately, this doesn't work. I'm not able to go into continueFileOpenPicker because the flag activationKind is always undefined. I'm pretty sure I should use app.onactivated instead of app.onloaded, but in the former case I'm not able to go into the function.
I already tried the function pickSinglePhoto and it seems to work, but I can't come back to the page after the selection since the app crashes, clearly because I can't pick and use the function in my others javascript files as a brute.
Any clue?