Silverlight Sample Application – Part 5

In this series blog post I’m completing this run about the sample Silverlight application that was used in the Ottawa and Toronto code camps this year.

You can get the source to the application on CodePlex.

The purpose of the application (other than to show people how to get started with Silverlight application development), was to be an IT event information application.  The fourth ‘page’ of the application shows a map of the area indicating the location of the event.

Here it is showing the location of the Ottawa IT Day event.

20100613 map

This was done using the Bing Maps Silverlight control.

There are 4 key resource links for using this control.

To use the control you:

  • Install the SDK (which will place a couple of important DLLs in a folder you specify when installing)
  • Add a reference to the SDK DLLs to your project.
  • Add the MapControl namespace to your page XAML

20100613 map ns

  • Add the control to your XAML

20100613 map xaml 

  • Setup the control how you link in your code behind file

20100613 map code

The ApplicationId is obtained from the developer portal where you’ll need to agree to various licensing conditions.  With the Id the control will show a message indicated that it needs to be licensed.

A specific ZoomLevel is set in this example and the view is set to road mode.

The Location is set up from properties on the Application class and initialised elsewhere in this case.

The map is centred on the desired location and a Pushpin added there too.

A piece of control tree is created in code and this is added to the map and positioned at the desired location.  See the interactive SDK to learn how to add information to the map.

This control is extremely powerful and can be seem working live in the Silverlight page designer inside Visual Studio, e.g. set the ZoomLevel as an attribute on the XAML map element and you’ll see the map zoom in live!

Quite straight forward and you can (as with any XAML-related coding) chose for the most party whether to place settings in XAML or in code.

My only real issue with the control is the licensing for use of the control on intranets.

This concludes the posts on the sample application.

Check out other series blog posts for previously posted information about this sample application.

Advertisement

Silverlight Sample Application – Part 4

In this series blog post I’m continuing to talk about the sample Silverlight application that was used in the Ottawa and Toronto code camps this year.

You can get the source to the application on CodePlex.

The purpose of the application (other than to show people how to get started with Silverlight application development), was to be an IT event information application.  The second ‘page’ of the application shows information on the tracks and sessions available.

Here it is showing session information for the Ottawa IT Day.

20100613 sessions

This data was published using an OData-based service which is included with the source code.

Silverlight 4 includes networking classes specifically for OData.  We can use the Add Service Reference feature to add proxy classes which in this case include EventDataContainer.

20100613 sessions code

Silverlight includes LINQ to OData client classes so we can create a LINQ query on the container client proxy class which will be translated (when enumerated) using the magic of IQueryable-based LINQ implementations into an OData URI.  We create a DataServiceCollection based on the Track proxy class and assign it to the ListBox so that results can be shown once this DataServiceCollection is populated.  We then set up a completion handler on the collection and then load the collection asynchronously which causes the LINQ query to be converted.

Once the query completes, we’ll have a set of Track objects in the collection that will appear bound in the UI.  For each Track object we then asynchronously load the Session information underneath each Track.

DataServiceCollection is great because it includes the INotifyCollectionChanged implementation that works with the Silverlight binding system to show the session information under each track as the data loads in.

Check out the code to see the bindings in the XAML.

Check out other series blog posts for more information about this sample application.

Silverlight Sample Application – Part 3

In this series blog post I’m continuing to talk about the sample Silverlight application that was used in the Ottawa and Toronto code camps this year.

You can get the source to the application on CodePlex.

The purpose of the application (other than to show people how to get started with Silverlight application development), was to be an IT event information application.  The third ‘page’ (yes – I’ll get to the second page in the next post) of the application shows tweets about the event.

Since the Ottawa and Toronto code camps happen in April and May, here’s the same page set up to show tweets tagged with #teched for this month’s Microsoft TechEd confeference.

20100613 tweets

To get the data, we can use one of the great methods available in Silverlight to get data – the WebClient class.  This class is often used when you are connecting to a RESTful service which is documented somewhere but is not self-described with metadata (like many SOAP/OData web service where Add Service Reference in Visual Studio can often be used to create a proxy class).

All network data access in Silveright must be asynchronous to avoid the UI from freezing up.

We create out WebClient, setup a completion event handler and start an asynchronous download of a string using the Twitter Search API.

20100613 tweet behind

Once the completion event fires, we make sure there’s no error and then pass off processing of the received string to a utility function to get back objects we can bind to on our ListBox.

The Twitter Search API response format is document (or discoverable at least) so we set up a class to hold the items of a tweet we are interested in, and use LINQ to XML code to pull out the appropriate XML elements from the Twitter API Atom feed.

 20100613 tweet util

It’s that simple when you get the hang of LINQ to XML.  It’s a must learn for the modern .NET developer.

With objects to bind to we need to create a DataTemplate for our ListBox ItemTemplate (the template used every time the visual tree for a new item needs to be generated).

20100613 tweet xaml

And there you have it.  It’s surprising there aren’t even more Twitter Clients coming out of the woodwork.

Check out other series blog posts for more information about this sample application.

Silverlight Sample Application – Part 2

In this series blog post I’m continuing to talk about the sample Silverlight application that was used in the Ottawa and Toronto code camps this year.

You can get the source to the application on CodePlex.

The purpose of the application (other than to show people how to get started with Silverlight application development), was to be an IT event information application.  The first ‘page’ of the application shows the event website.

20100613 home

To do this, our page simply includes the web browser control in the XAML…

20100613 webbrowser

and a bit of code in the code-behind class also inheriting from the provided Page class constructor.

20100613 webbrowser 2

Simple enough right?

Wrong.

This code will fail when you try to debug/launch the application in a web browser.  The web browser control requires the application to be installed out-of-browser by the user and with elevated full trust.

20100613 oob

In the project properties, running Out-of-Browser needs to be enabled, the settings configured and the “Require elevated trust when running outside the browser” needs to be checked.

So HTML content and be hosted alongside the Silverlight control in a web page, but to host HTML content inside the Silverlight control you need the user to agree to the elevated trust and run the application outside of the browser.

To avoid making this page useless when the application is run inside the web browser, code was added as follows:

20100613 oob detect

This code detects if the code is running out of browser or not.  If it’s in-browser then the browser control is hidden, and a hyperlink control added to the main grid allowing the user to click it and be taken to a web page using their default web browser.

Be sure to check out the other features a Silverlight 4 application can use if it runs out of browser.

Check out other series blog posts for more information about this sample application.

Silverlight Sample Application – Part 1

In this series blog post I’m starting a new set to talk about the sample Silverlight application that was used in the Ottawa and Toronto code camps this year.

I was given the whole Silverlight track to develop and lead in Ottawa and decided that I wanted to use the opportunity to show how a complete sample application can be built, deployed and adapted for Windows Phone 7.  At the same time I wanted to introduce many of the topics involved with Silverlight development.

Below is the chart showing the 5 sessions presented along with a section at the bottom showing a few other major topics we didn’t have time to include.

20100613 topics 

So, a fair challenge to pull off in 5 1-hour sessions.

This was made possible through the skills of my fellow track presenters: Peter Henry, Sylvain Boucher & Louis-Philippe Pinsonneault.  In particular Peter was my right-hand man and come up with the application concept.

I also repeated the Essentials and Data Matters sessions at the Toronto Code Camp.

You can get the source to the application on CodePlex.

In the next few posts I’m going to explain how parts of the application were built including use of the web browser control and out-of-browser applications…

20100613 home

using LINQ to XML to get data from Twitter…

20100613 tweets

connecting and Binding to OData…

20100613 sessions

and using the Bing Maps Silverlight Control…

 20100613 map  00

Overall the application leveraged the Silverlight Navigation Application project template for navigation.  The template is provided with the Silverlight Tools for Visual Studio.

So follow the series posts to learn how it was done.

In a later set of posts I’ll talk more about Silverlight data in more detail.