Page 70 - MSDN Magazine, July 2017
P. 70
Modern Apps
Launch Other Applications from Your UWP App
Windows has long provided the capability to programmatically launch applications from within another application. Indeed, it’s a feature common to practically any OS. The Universal Windows Platform (UWP) is no exception. In fact, the UWP adds a few new features. In this month’s column, I’ll explore the Launcher class and how to put it to use in your UWP apps.
In my last two columns, I wrote
about exploring the rich function-
ality of the Bing Maps Control for
UWP apps. There might be cases
where you don’t need or wish to
embed a rich mapping solution
inside your UWP app. For those
scenarios, you can leverage the
built-in Maps app. This solution
is ideal if you want to provide your users with mapping tools, but don’t wish to add complexity to your code base. Accomplishing this is easy with the Launcher class.
Use a Map Without Using a Map Control
Create a new blank UWP project in Visual Studio by choosing New Project from the File menu. Expand the Installed Templates | Windows | Blank App (Universal Windows). Name the project MapLauncher and then click OK. Immediately afterward, a dialog box will appear asking you which version of Windows the app should target. For this project, the default options will be fine, so you can just click OK. In the MainPage.xaml file, add the following XAML to create a button control:
<Button Name="btnLaunchMap" Click="btnLaunchMap_Click">Launch Map</Button>
In the MainPage.xaml.cs file, add the following code for the event handler:
private async void btnLaunchMap_Click(object sender, RoutedEventArgs e) {
Uri uri = new Uri("bingmaps:?rtp=adr.Washington,%20DC~adr.New%20York, %20NY&mode=d&trfc=1");
await Launcher.LaunchUriAsync(uri); }
FRANK LA VIGNE
Figure 1 Driving Directions Between New York City and Washington, D.C., in the Maps App
Run the solution and click on the button. The Maps app should launch and display driving directions between Washington, D.C., and New York City along with traffic information, as shown in Figure 1.
It’s quite impressive what can be done in so few lines of code. What, exactly, is going on here that made this so simple and why did the default Maps app automatically launch?
Protocol Launching
Protocol launching enabled launching the Maps app pre- populated with a route. Windows has always relied on file extensions and file-type associations to determine what applica- tions should launch when a file is run. Starting with Windows 8, protocol launching was added to help facilitate launching apps with the option of passing along parameters.
Launching Apps via URI
Taking a closer look at the code in the previous sample, there’s something familiar about the line of code that creates a URI. The string passed to the URI constructor method follows a pattern familiar to Web developers, yet with a few differences.
Web addresses or, specifically, URIs, follow a pattern of [proto- col]:[host address]?[parameter1]=[value1]& [parameter2]=[value2].
Code download available at bit.ly/2qkp2hz.
64 msdn magazine