Windows 8 JavaScript Sharing Charm - "This app can't share."

0

My Windows 8 app allows sharing on 1 page (the high scores page) which worked fine but since the sharing code was only on the high scores page the app said "This app can't share." when the user tried to share on a page other than the high scores page.

It should say "There's nothing to share right now." To fix this I added sharing code to the home page, this fixed the issue but broke the sharing on the high scores page, I get this error "0x8000000e - JavaScript runtime error: A method was called at an unexpected time."

My JavaScript code for the home page is:

(function () {
    "use strict";

    // SHARING //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    var storage = Windows.Storage;
    var dtm2 = Windows.ApplicationModel.DataTransfer.DataTransferManager;
    // SHARING //////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    WinJS.UI.Pages.define("/pages/home/home.html", {
        // This function is called whenever a user navigates to this page. It
        // populates the page elements with the app's data.
        ready: function (element, options) {
            // TODO: Initialize the page here.

            // Initialize everything
            initialize();

            // Button listeners
            var add = document.getElementById("add");
            add.addEventListener("click", this.linkClickEventHandler, false);

            var sub = document.getElementById("sub");
            sub.addEventListener("click", this.linkClickEventHandler, false);

            var mul = document.getElementById("mul");
            mul.addEventListener("click", this.linkClickEventHandler, false);

            var div = document.getElementById("div");
            div.addEventListener("click", this.linkClickEventHandler, false);

            // SHARING //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            dtm2.getForCurrentView().addEventListener("datarequested", this.onDataRequested);
            // SHARING //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        },

        linkClickEventHandler: function (eventInfo) {
            ope = this.id;
            var link = "/pages/quiz/quiz.html";
            WinJS.Navigation.navigate(link);
        },

        // SHARING //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        onDataRequested: function (e) {

        },
        // SHARING //////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        unload: function () {
            // TODO: Respond to navigations away from this page.

            // SHARING //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            WinJS.Navigation.removeEventListener("datarequested", this.onDataRequested);
            // SHARING //////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        },
    });
})();

My JavaScript code for the high scores page is:

// For an introduction to the Page Control template, see the following documentation:
// http://go.microsoft.com/fwlink/?LinkId=232511
(function () {
    "use strict";

    // SHARING //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    var storage = Windows.Storage;
    var dtm = Windows.ApplicationModel.DataTransfer.DataTransferManager;
    // SHARING //////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    WinJS.UI.Pages.define("/pages/results/results.html", {
        // This function is called whenever a user navigates to this page. It
        // populates the page elements with the app's data.
        ready: function (element, options) {
            // TODO: Initialize the page here.

            // Show results
            showResults();

            // Button listeners
            var btnHome = document.getElementById("btnHome");
            btnHome.addEventListener("click", this.linkClickEventHandler, false);

            // SHARING //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            dtm.getForCurrentView().addEventListener("datarequested", this.onDataRequested);
            // SHARING //////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        },

        linkClickEventHandler: function (eventInfo) {
            var link = "/pages/home/home.html";
            WinJS.Navigation.navigate(link);
        },

        // SHARING //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        onDataRequested: function (e) {
            var request = e.request;
            request.data.properties.title = "Your Simple Math Results";

            var message = shareLine1 + " ";
            message += shareLine2 + " ";
            message += shareLine3;
            request.data.setText(message);
        },
        // SHARING //////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        unload: function () {
            // TODO: Respond to navigations away from this page.

            // SHARING //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            WinJS.Navigation.removeEventListener("datarequested", this.onDataRequested);
            // SHARING //////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        },

        updateLayout: function (element, viewState, lastViewState) {
            /// <param name="element" domElement="true" />

            // TODO: Respond to changes in viewState.
        }
    });
})();
windows-8
asked on Stack Overflow Dec 5, 2012 by user1822824

1 Answer

1

You need to unattach the "datarequested" event when you navigate out of the page. This is, when the back button is pressed:

dtm2.getForCurrentView().removeEventListener("datarequested", this.onDataRequested);
answered on Stack Overflow Dec 6, 2012 by danielrozo

User contributions licensed under CC BY-SA 3.0