Building an Open Data App – Part 6: Launchers

As of right now, we have a simple list of the various bike stations in the Toronto Bike Share network. Ideally, we’d be able to tap on a location and see where it is on a map, maybe even get directions. So let’s do that. We’ll add an event to the list items and launch the user’s default mapping software with the required data to display the location.

As we’re working in a Universal App, we’ll need to make use of URI Launchers. Additionally, we can’t target a specific app unless it has a URI scheme associated with it. As a first step in this process, let’s add a Tapped event to the ListBoxItem in the XAML.

<br>&lt;ListBoxItem Tapped="BikeStation_Tapped" Tag="{Binding}"&gt;<br>

You’ll notice the presence of the Tapped event and Tag property. The Tapped event is fairly self explanatory, hold the name of the associated event. The Tag, however, is capable of holding an object. In this case, we’ve set it to hold the actual BikeStation object that is acting as the data context for the ListBoxItem. While not necessary, you’ll see in the event handler why this is an expedient.

<br>private async void BikeStation_Tapped(object sender, TappedRoutedEventArgs e)<br>{<br>var bikeStation = (sender as ListBoxItem).Tag as BikeStation;</p> <p>await Windows.System.Launcher.LaunchUriAsync(new Uri(string.Format("bingmaps:?cp={0}~-{1}", bikeStation.Longitude, bikeStation.Latitude)));<br>}<br>

We’ve added the above event handler to the code behind of the ListPage. As you can see, we’re using the ListBoxItem Tag to quickly give us full access to the BikeStation‘s fields. In this case, the longitude and latitude of the station. The URI you see is based on an established schema for mapping provided by Microsoft. If you prefer, you could provide another URI to launch a webpage, or if you have an app with a URI scheme associated, you could use that.

This method call will launch the user’s default map viewing program. If no default exists, the user will see the launch prompter or be asked to get an app from the store. If you’d like to see the prompter regardless of default, you would add the LauncherOptions argument to the call. Additional options are available with that.

Now that our app has a touch of functionality, we’ll move on to styling and giving the user a little more information. We’ll even look at using converters to dynamically affect the display of station information. But that will be in the next post.

This entry was posted in .NET, Building an Open Data App, Open Data, Windows 8, Windows 8.x, Windows Phone and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s