Reader Level:
Article

Receiving URI in Windows Store Apps

By Deepak Middha on Nov 22, 2012
In this app I describe receiving a URI in a Windows Store app using JavaScript.

Introduction

In this article I describe how to create a Windows Store App for receiving a URI using JavaScript. In File Explorer, create a file and double-click the file. This application is brought to the foreground. The URI is displayed in the output field. In my previous article I described how to launch a URI in Windows Store apps. For this visit, Launching a uri in windows Store using Java Script.

I assume you can create a simple Windows Store App using JavaScript. For more help visit Simple Windows Store Apps using JavaScript.

To start the creation of the app, add one JavaScript page by right-clicking on the js folder in the Solution Explorer and select Add > new item > JavaScript Page and then provide an appropriate name. In the same way, add one HTML page to your project.

receving-file-in-windows-store-apps.jpg

Write the following code in default.html:

<!DOCTYPE html>

<html>

<head>

    <meta charset="utf-8" />

    <title></title>

    <link rel="stylesheet" href="//Microsoft.WinJS.1.0/css/ui-light.css" />

    <script src="//Microsoft.WinJS.1.0/js/base.js"></script>

    <script src="//Microsoft.WinJS.1.0/js/ui.js"></script>

    <link rel="stylesheet" href="/css/default.css" />

    <script src="/js/scriptjs"></script>

    <script src="/js/default.js"></script>

</head>

<body role="application">

    <div id="rootGrid">

        <div id="content">

            <h1 id="featureLabel"></h1>

            <div id="contentHost"></div>

        </div>

    </div>

</body>

</html>


Write the following code in default.js:

(function () {

    "use strict";

    var appTitle = "";

    var pages = [

        { url: "page.html" }

    ];

    function activated(e) {

        var url = null;

        var arg = null;

        if (e.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.file) {

            url = pages[2].url;

            arg = e.detail.files;

        } else if (e.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.protocol) {

            url = pages[3].url;

            arg = e.detail.uri;

        } else if (e.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch) {

            url = WinJS.Application.sessionState.lastUrl || pages[0].url;

        }

        if (url !== null) {

            e.setPromise(WinJS.UI.processAll().then(function () {

                return WinJS.Navigation.navigate(url, arg);

            }));

        }

    }

    WinJS.Navigation.addEventListener("navigated", function (evt) {

        var url = evt.detail.location;

        var host = document.getElementById("contentHost");

        host.winControl && host.winControl.unload && host.winControl.unload();

        WinJS.Utilities.empty(host);

        WinJS.UI.Pages.render(url, host, evt.detail.state).done(function () {

            WinJS.Application.sessionState.lastUrl = url;

        });

    });

    WinJS.Namespace.define("App", {

        appTitle: appTitle,

        pages: pages

    });

    WinJS.Application.addEventListener("activated", activated, false);

    WinJS.Application.start();

})();


Write the following code in page.html:
 

<!DOCTYPE html>

<html>

<head>

    <title></title>

    <script src="/js/script.js"></script>

</head>

<body>

    <div data-win-control="App.pageOutput">

    </div>

</body>

</html>


Write the following code in script.html:

(function () {

    "use strict";

    var page = WinJS.UI.Pages.define("page.html", {

        processed: function (element, uri) {

            if (uri) {

                WinJS.log && WinJS.log("Protocol activation received. The received URI is " + uri.rawUri + ".", "app", "status");

            }

        },

        ready: function (element, options) {

        }

    });

})();


Summary

In this app I described receiving a URI in a Windows Store app using JavaScript. I hope this article has helped you to understand this topic. Please share if you know more about this. Your feedback and constructive contributions are welcome.

Deepak Middha

Deepak Middha is one time Microsoft MVP(Client App Dev) , working as Sr. Software Developer with lots of hands on experience using ASP.NET 2.0/3.5, AJAX, MVC, C#,  SQL Server 2005/2008/2012, WCF, JQuery and Silverl... Read more

COMMENT USING