Page 75 - MSDN Magazine, July 2017
P. 75

Advanced Launch Features
Thus far, the examples in the FoodFinder app had specific outcomes in mind. I had intended to launch the default Maps app and the Windows Store app. In cases where developers want to customize the launch- ing experience or give the user a choice in which app launches, the Launcher class exposes additional features through the use of the LauncherOptions class.
Choosing an Application
to Launch
From time to time, users will be present-
ed an option to pick which app to launch
when there are multiple apps registered to
handle a specific file extension or protocol.
For example, it’s very common for users to
have multiple browsers installed on their systems. In fact, Windows 10 comes with Edge and Internet Explorer. Giving users a choice in which application to launch would be ideal in particular use cases. Fortunately, the LauncherOptions class in the Windows.System namespace makes it easy to customize how the Launcher class acts.
In the MainPage.xaml file for the FoodFinder app, add the following XAMLintheStackPaneltoaddanewbuttontoperformWebsearches: <Button Name="btnSearchWeb" Click="btnSearchWeb_Click" Margin="5">Search Web</Button> Now, add the following event handler code in the
MainPage.xaml.cs file:
private async void btnSearchWeb_Click(object sender, RoutedEventArgs e) {
string genre = (cbxGenre.SelectedItem as ComboBoxItem).Content.ToString(); string queryString = Uri.EscapeDataString($"{genre} in {txtLocation.Text}"); var searchUri = $"https://www.bing.com/search?q={queryString}";
var uriBing = new Uri(searchUri);
var promptOptions = new Windows.System.LauncherOptions(); promptOptions.DisplayApplicationPicker = true;
var success = await Windows.System.Launcher.LaunchUriAsync(uriBing, promptOptions);
}
Just as before, this code constructs a URI based on the choice in the combo box and value entered into the textbox. The addition of the LauncherOptions class and setting the DisplayApplication- Picker property to true will trigger a prompt similar to Figure 7. Run the app now, enter some values and click Search Web to see the Application Picker Prompt.
By default, the DisplayApplicationPicker is set to false, which launches the default browser on the system.
Confirming Launch
It’s important to point out that, in certain situations, Windows will display a prompt to confirm if the user actually wishes to switch apps, as shown in Figure 8. How- ever, UWP developers cannot suppress this dialog. This is an important security precaution to prevent users from running malicious code.
There might be times when it’s advanta- geous to confirm with the user that the app intends to switch apps. For these cases, set the TreatAsUntrusted property to “true.”
Remove the line where the Display- ApplicationPicker property is set to true and add this line to the btnSearchWeb_ Click event handler immediately after the declaration of the promptOptions variable:
promptOptions.TreatAsUntrusted = true;
Run the solution again, enter a value in the Location textbox, and click the Search Web button. You’ll see a prompt prior to the browser launching. If the DisplayApplicationPicker and the TreatAsUnstrusted properties are both set to true, then only the application picker prompt will display. This streamlines the pro- cess for the user, who would likely get frustrated at the process of clickingthroughmultipledialogboxestoperformatask.
The LauncherOptions class exposes much more functionality and provides flexibility for any number of use-case scenarios.
The LauncherOptions class exposes much more functionality and provides flexibility for any number of use-case scenarios. You can find out more at bit.ly/2qoW8LN.
Wrapping Up
In this column, I demonstrated how to use protocol activation in UWP apps to leverage the power of other apps available on the user’s device. This lets you add rich features and conveniences without inserting complexity to its code base. The UWP offers
great flexibility via the LauncherOptions class, which lets developers customize the UX. n
Frank La Vigne is chief evangelist at DataLeader.io, where he helps customers leverage technology in order to create a better world. He is co-host of the DataDriven podcast and blogs regularly at FranksWorld.com. He has a YouTube channel called Frank’s World TV (FranksWorld.TV).
Figure 7 Application Picker Prompt
Figure 8 Prompt Confirming Desire to Switch Apps msdnmagazine.com
Thanks to the following technical expert for reviewing this article: Jose Luis Manners
July 2017 69































































   73   74   75   76   77