Page 72 - MSDN Magazine, September 2017
P. 72

Modern Apps FRANK LA VIGNE Protocol Registration and Activation
in UWP Apps
In my last column, I explored how to use the Launcher class to extend the functionality of a Universal Windows Platform (UWP) app by leveraging the features of other apps built into Windows. For example, I was able to add fully featured map­ ping functionality, such as searching for a good place to find pizza and finding directions complete with traffic reports, without having to add significant complexity to my app’s code base. In this month’s col­ umn, I’ll explore the other side of the equation: How to expose a UWP app’s functionality to other apps.
Of course, programmatically
launching applications and passing
parameters along from other appli­ cationspredatestheUWP.Infact,
many Windows apps support this
via command­line switches. For
example, to launch Microsoft Word in Safe Mode, where add­ ons and plug­ins are disabled, pass the parameter “/safe” to the WinWord.exe file. To test this out, press Windows Key+R, to bring up the Run dialog box. In the textbox, type “Winword.exe /safe” and click OK. If you have Microsoft Word installed on your sys­ tem, then the program will launch in Safe Mode. Additionally, similar commands exist for Excel, PowerPoint and others. Clearly, launching a program with parameters isn’t a new mechanism. What’s new, however, is the additional mechanism that UWP provides: Uniform Resource Identifier (URI) activation.
URI Activation
In my last column (msdn.com/magazine/mt784669), I used URI activa­ tion to launch and pass parameters to several apps. The apps then took these input parameters and acted upon them. For example, to get driving directions between Washington, D.C., and New York City with the default Maps app in Windows 10, the app simply
passes the following URI to the Launcher class:
bingmaps:?rtp=adr.Washington,%20DC~adr.New%20York,%20 NY&mode=d&trfc=1
Figure 1 Adding a Protocol Declaration (Note the Validation Errors)
Alternatively, typing this command into the Run dialog box will also yield the same result. The Maps app has been registered to handle the “bingmaps” protocol. The text after the question mark represents parameters passed along to the Maps app. In this case, it sets the start and end points, along with a mode to indicate driving (“d”) and a 1 to add traffic data to the results.
What if you wanted your app to perform in a similar manner? What are the steps you’d need to take in order to register a protocol and handle the subsequent parameters passed to it?
The first step is to create a new project. Create a new blank UWP project in Visual Studio by choosing New Project from the File menu. Expand Installed Templates | Windows | Blank App (Universal Windows). Name the project ProtocolActivation and then click OK. Immediately afterward, a dialog box will appear asking you which version of Windows the app should target. For this proj­ ect, the default options will be fine, so you can simply click OK.
Registering a URI Protocol
Now that the project has been created, open the appmanifest to register a protocol. In the Solution explorer, look for the Package.appmanifest file and double­click on it to open it. Click on the Declarations tab. Choose Protocol from the dropdown underneath Available
Code download available at bit.ly/2gOkEEZ.
64 msdn magazine














































































   70   71   72   73   74