Page 74 - MSDN Magazine, September 2017
P. 74

Declarations. Now, click Add to add the declaration to the mani­ fest. The screen should look similar to Figure 1.
While it’s highly recommended to provide entries for the Logo and Display name fields, the only required field is Name. Enter “bingmaps” into the textbox. Press the Tab key. Note that the valida­ tion errors disappear after the textbox loses focus. Run the project by pressing F5, choosing Start Debugging from the Debug Menu, or simply clicking on the play icon on the toolbar. Once the app is running, press Windows Key+R to launch the Run dialog. Enter “bingmaps:” into the textbox and click OK. Note that the default Maps app started just as before.
While highly recommended to provide entries for the Logo and Display name fields, the only required field is Name.
This brings up an important caveat: Some protocols cannot be overridden. For a complete list, either refer to my previous column or refer to the MSDN documentation on Reserved File and URI Scheme Names at bit.ly/2st28Er. Choosing any of these reserved names will not result in an error message; the
Handling Activation
UWP apps can be activated any number of ways, from being launched by the user when its tile is tapped or when activated by protocol. In order to detect how the app was launched, the OnActivated event handler must be overridden.
In Solution Explorer, open the App.xaml.cs file, and add the
following code to the App class:
protected async override void OnActivated(IActivatedEventArgs e) {
if (e.Kind == ActivationKind.Protocol) {
var dialog = new MessageDialog("App Activated by Protocol.");
await dialog.ShowAsync(); }
Window.Current.Activate(); }
Run the solution once again, open the Run dialog, type in “msdncolors:” and click OK. This time, you’ll see a message dia­ log. Stop the app from running. Now, re­open the Run dialog by pressing Windows Key+R, enter “msdncolors:” into the textbox and click OK. Note that the same message dialog appears, but the app doesn’t progress beyond the splash screen. The reason why will be addressed later.
Passing Parameters via URI
While launching an app through URI activation has its uses, the real power comes in passing parameters from one app to another. This can be done by appending parameters to the protocol request.
app simply won’t get activated.
Registering a Custom
Protocol
Close the app if it’s still running. Go back into the manifest file and change the contents of the Name field to “msdn­ colors” and save the project. Now, press Windows Key+R to bring up the Run dialog, enter “msdncolors” into the textbox and click OK. A system­wide dialog box appears suggesting to look for an app in the Store that can handle this protocol, as shown in Figure 2.
While the protocol might have been declared in the app’s manifest file, it hasn’t yet been registered on the system. This happens at install time. Run the solution now and then enter “msdncolors” into the Run dialog once again. The same dialog box appears, but with a new option added: the ProtocolActivation app, as shown in Figure 3. Click OK. The ProtocolActivation app will launch. Close the app. Enter “msdncolors” once more into the Run dialog box. This time, there will be no dialog box, the app will simply run whether the “Always use this app” option was checked.
Figure 2 Dialog Box That Appears When the System Sees an Unfamiliar Protocol
This mechanism operates nearly the same way as HTTP GET requests.
For example, URIs follow a pattern of: [protocol]:[host address]?[parame­ ter1]=[value1]& [parameter2]=[value2]
For instance, given the URI http:// bing.com/search?q=data%20driven%­ 20podcast,theprotocolisHTTP,the host address is bing.com, and the value of “data%20driven%20podcast” is passed to the parameter “q.” Note that the spaces in “data driven podcast” are converted to “%20.” Parameter names and values passed along in a URI must be encoded.
Modify the code inside the OnActi­ vated method to contain the changes shown in Figure 4.
The code in Figure 4 casts the IActivatedEventArgs parameter to a ProtocolActivatedEventArgs class, providing access to a number of prop­ erties specific to protocol activation scenarios. In this instance, I’m main­ ly concerned with the URI property. The URI will contain the information passed to the app via protocol acti­ vation. Run the app again, open the Run dialog, enter “msdncolors:back­ ground?red” into the textbox, and
66 msdn magazine
Modern Apps
Figure 3 The New App Now Appears as an Option





































































   72   73   74   75   76