DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Launching URIs in a Windows Store App

Gil Fink user avatar by
Gil Fink
·
Dec. 31, 12 · Interview
Like (0)
Save
Tweet
Share
5.93K Views

Join the DZone community and get the full member experience.

Join For Free

During the writing of a Windows Store app, we got a requirement to launch a web page from the app. Launching a web page means opening a browser with a given URI. So how can you accomplish that? The answer is very simple and is explained in this post.

The Windows.System.Launcher Namespace

In Windows Store apps you can use the WinRT Windows.System.Launcher namespace to launch web pages or files. The namespace includes the following API functions:

  • launchUriAsync – receives a Windows.Foundation.Uri and open it in the app associated with the URI scheme (in most cases a browser).
  • launchFileAsync – receives a IStorageFile and start it using the default app that is associated with the file type.

The following example shows how to launch a URI:

Windows.System.Launcher.launchUriAsync(linkURI);

Both of the functions can receive an additional LauncherOptions parameter. The LauncherOptions can help to configure the launch with options like content type or display application picker. Here is a simple example that will open the application picker:

var options = new Windows.System.LauncherOptions();
options.displayApplicationPicker = true;

Windows.System.Launcher.launchUriAsync(uri, options);

Launching a URI

Formatting a URI String In the example I’m going to use a string format function. The function receives string arguments and replace placeholders, represented with curly brackets and an argument index number, with the associated arguments. The format function is added to the string prototype and it is very useful in other situations:

String.prototype.format = function () {
    var str = this;
    for (var i = 0; i < arguments.length; i++) {
        var reg = new RegExp("\\{" + i + "\\}", "gm");
        str = str.replace(reg, arguments[i]);
    }

    return str;
};

Launching a formatted URI

In the example I’m using the following URI:

var uri = https://www.google.co.il/search?q={0};

I could have used string concatenation to create the URI but in real world scenarios you are probably going to format a more sophisticated URI so I decided to use the format function instead. 
Here is the code to launch a search in Google:

function launch(query) {
    var queryUri = getQueryUri(query);
    launcher.launchUriAsync(queryUri);
};
 
function getQueryUri(query) {
    var formattedUri = uri.format(query);
    return new Windows.Foundation.Uri(formattedUri);
};

The Full Example

Here is the entire code for a launchManager:

(function () {
    "use strict";
 
    var launcher = Windows.System.Launcher;
    var uri = "https://www.google.co.il/search?q={0}";
 
    String.prototype.format = function () {
        var str = this;
        for (var i = 0; i < arguments.length; i++) {
            var reg = new RegExp("\\{" + i + "\\}", "gm");
            str = str.replace(reg, arguments[i]);
        }
 
        return str;
    };
 
    var launcherManager = WinJS.Class.define(function () {
        var self = this;
 
        self.launch = function (query) {
            var queryUri = getQueryUri(query);
            launcher.launchUriAsync(queryUri);
        };
 
        function getQueryUri(query) {
            var formattedUri = uri.format(query);
            return new Windows.Foundation.Uri(formattedUri);
        };
    },
    {
    });
 
    WinJS.Namespace.define("Launcher", {
        launcherManager: new launcherManager()
    });
}());

For the simplicity of the example I’ve put the string format function inside of the scope of the Launcher namespace. In a real app the string format function should exist in a helpers JavaScript file.

Now you can use the Launcher namespace like in the following example:
Launcher.launcherManager.launch("HTML5");
Summary

In order to launch files or web pages you can use the Windows.System.Launcher namespace. In the post I showed how to launch an app associated with a given URI scheme.

app

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Writing a Modern HTTP(S) Tunnel in Rust
  • A Simple Union Between .NET Core and Python
  • SAST: How Code Analysis Tools Look for Security Flaws
  • How To Generate Code Coverage Report Using JaCoCo-Maven Plugin

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: