Page 72 - MSDN Magazine, July 2017
P. 72

Figure 2 Default Protocols in Windows
Windows 10 and may be leveraged outside of the UWP. In fact, Windows comes with a series of protocols already defined. The entire list can be found in Figure 2. Note that some of the pro- tocols, like ms-call, may only be available on Windows Mobile.
Passing Parameters
As for the Maps app, there’s any number of parameters to pass and the Maps app can be controlled to fit any app’s specific scenario. For instance, if the app needs to find pizza places in the Bronx, you’d simply use the following URI:
bingmaps:?q=pizza&where=Bronx,NY
Food Finder App
To demonstrate how to put parameters and protocol launching to practical use, create a new blank UWP project in Visual Studio. Name the project FoodFinder and click OK to create the app.
Add the XAML shown in Figure 3 to the MainPage.xaml file. As for the Maps app, there’s
any number of parameters to pass and the Map app can be controlled to fit any app’s specific scenario.
In the MainPage.xaml.cs file add the following code for the btnLaunchMap_Click event handler:
private async void btnLaunchMap_Click(object sender, RoutedEventArgs e) {
string genre = (cbxGenre.SelectedItem as ComboBoxItem).Content.ToString(); string trafficValue = (ckbTraffic.IsChecked.Value) ? "1" : "0";
string uriString = string.Format( $"bingmaps:?q={genre}&where={txtLocation.Text}&lvl=15&trfc={trafficValue}");
Uri uri = new Uri(uriString);
await Launcher.LaunchUriAsync(uri); }
Run the project now. The interface should look similar to what’s shown in Figure 4. Pick a genre of food, enter a city into the Location textbox, and click Launch Map. The Maps app will launch and display a result set based on the inputs. Naturally, search results will vary based on the genre chosen and location entered. The final result should look something like Figure 5.
URI Scheme
Launches
bingmaps:, ms-drive-to:, and ms-walk-to:
Maps app
http:
Default Web browser
mailto:
Default e-mail app
ms-call:
Call app
ms-chat:
Messaging app
ms-people:
People app
ms-settings:
Settings app
ms-store:
Store app
ms-tonepicker:
Tone picker
ms-yellowpage:
Nearby Numbers app
For instance, for the URI http://bing.com/search?q=franksworld, the protocol is HTTP, the host address is bing.com, and the value of “franksworld” is passed to the parameter “q.” The same holds true for the following URI, but with a few differences:
bingmaps:?rtp=adr.Washington,%20DC~adr.New%20York,%20NY&mode=d&trfc=1
Unlike HTTP or HTTPS, the protocol “bingmaps” is probably unfamiliar to most readers. Also, there’s no address for the resource. Interestingly, typing the this URI into the Edge browser will have the same effect, launching the Bing Maps app with the route between Washington, D.C., and New York City pre-populated. Additionally, the Run dialog, accessed by Windows key+R, will do the same if that URI is entered. Protocol activation is part of
Figure 3 XAML Code to Create the UI
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Grid.ColumnDefinitions>
<ColumnDefinition Width="43*"/>
<ColumnDefinition Width="137*"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="45*"/> <RowDefinition Height="44*"/> <RowDefinition Height="45*"/> <RowDefinition Height="45*"/> <RowDefinition Height="461*"/>
</Grid.RowDefinitions>
<TextBlock Margin="10" FontSize="24" Grid.RowSpan="5" Grid.ColumnSpan="2">Food Finder</TextBlock>
<TextBlock Grid.Row="1" HorizontalAlignment="Right" VerticalAlignment="Center">Genre</TextBlock>
<TextBlock Grid.Row="2" HorizontalAlignment="Right" VerticalAlignment="Center">Location</TextBlock>
<ComboBox Name="cbxGenre" Grid.Row="1" Grid.Column="1" VerticalAlignment="Center" Margin="5" Width="212" SelectedIndex="0"> <ComboBoxItem>Pizza</ComboBoxItem>
<ComboBoxItem>BBQ</ComboBoxItem> <ComboBoxItem>Coffee</ComboBoxItem>
</ComboBox>
<TextBox Name="txtLocation" Grid.Row="2" Grid.Column="1"
Margin="5"></TextBox>
<CheckBox Name="ckbTraffic" Grid.Row="3" Grid.Column="1"
Margin="5">Show Traffic</CheckBox>
<StackPanel Orientation="Horizontal" Grid.Row="4"
Grid.ColumnSpan="2" HorizontalAlignment="Center" VerticalAlignment="Top">
<Button Name="btnLaunchMap" Click="btnLaunchMap_Click"
Margin="5">Launch Map</Button>
<Button Name="btnSearchMusic" Click="btnSearchMusic_Click"
Margin="5">Search Music</Button> </StackPanel>
</Grid>
66 msdn magazine
Modern Apps
Figure 4 FoodFinder Interface
































   70   71   72   73   74