Book Image

Microsoft Silverlight 4 Data and Services Cookbook

Book Image

Microsoft Silverlight 4 Data and Services Cookbook

Overview of this book

Microsoft Silverlight is a programmable web browser plugin that enables features including animation, vector graphics, and audio-video playback – features that characterize Rich Internet Applications. However, Silverlight is a great (and growing) Line-Of-Business platform and is increasingly being used to build data-driven business applications. Silverlight Data Services enable efficient access to your data, allowing you to draw on multiple sources of data and solve particular data problems. There is very little existing material that demonstrates how to build data-driven solutions with the platform. Silverlight 3 made a big step into Line-Of-Business data services and Silverlight 4 builds further upon this. This book will enable .NET developers to get their finger on the pulse of data-driven business applications in Silverlight.This book is not a general Silverlight 3/4 overview book; it is uniquely aimed at developers who want to build data-driven applications. It focuses on showing .NET developers how to interact with, and handle multiple sources of data in Silverlight business applications, and how to solve particular data problems following a practical hands-on approach, using real-world recipes. It is a practical cookbook that teaches you how to build data-rich business applications with Silverlight that draw on multiple sources of data. Most of the covered features work both in Silverlight 3 and 4. However, we cover some that are specific for Silverlight 4, which will therefore not work with Silverlight 3. Where this is the case, it is clearly indicated.Packed with reusable, real-world recipes, the book begins by introducing you to general principles when programming Silverlight. It then dives deep into the world of data services, covering all the options available to access data and communicate with services to get the most out of data in your Silverlight business applications, whilst at the same time providing a rich user experience. Chapters cover data binding, data controls, the concepts of talking to services, communicating with WCF, ASMX and REST services, and much more.By following the practical recipes in this book, which are of varying difficulty levels, you will learn the concepts needed to create data-rich business applications—from the creation of a Silverlight application, to displaying data in the Silverlight application and upgrading your existing applications to use Silverlight. Each recipe covers a data services topic, going from the description of the problem, through a conceptual solution to a solution containing sample code.
Table of Contents (16 chapters)
Silverlight 4 Data and Services Cookbook
Credits
About the authors
About the reviewers
Preface

Using the workflow between Visual Studio 2010 and Blend 4


Expression Blend (currently at version 4) is part of Microsoft's Expression Suite. It's a designer tool that allows designers to create compelling user experiences for use with WPF and Silverlight. While aimed at designers, it's a tool that should be in a Silverlight developer's toolbox as well. In some areas, it offers a richer designer experience than Visual Studio does. One of the best examples of this is the timeline that makes it easy to create time-based animations.

In this recipe, we'll look at how Visual Studio and Blend integrate. When used together, they help us create our applications faster. In the next chapter, we'll take another look at Blend—namely at its features that support data binding.

Getting ready

After having read the Getting our environment ready to start developing Silverlight applications recipe, you should have Expression Blend 4 installed.

In this recipe, we are creating the sample from scratch. The completed solution can be found in the Chapter01/Blend folder in the code bundle that is available on the Packt website.

How to do it...

In this recipe, we'll recreate the Hotel Browser application. However, we'll do most of the work from Blend and switch back and forth to Visual Studio when it is recommended. We'll need to carry out the following steps:

  1. 1. Although we can start a new solution from Blend, we'll let Visual Studio create the solution for us. The main reason is that we'll be using services later on in this sample and working with services is easier if the hosting site is an ASP.NET web application. Adding an ASP.NET web application is possible from Visual Studio, but not from Blend. Therefore, open Visual Studio 2010 and create a new Silverlight solution. Name it SilverlightHotelBrowser and make sure to select ASP.NET Web Application Project as the hosting website.

  2. 2. With the solution created in Visual Studio, right-click on one of the XAML files in the Silverlight project. In the context menu, select Open in Expression Blend... as shown in the following screenshot:

  1. 3. Expression Blend will open up and its workspace will be shown. The following is a screenshot of the interface containing some named references:

    The following table describes some of the most important items on the Blend workspace:

Item

Name

Description

1

Projects window

Gives an overview of the loaded solution and its projects. It is comparable to the Solution Explorer in Visual Studio.

2

Objects and Timeline

By default, this window gives an overview of all the XAML objects in the currently loaded document. When we want to perform any action on an item (such as giving it a background color), we select it in the Objects and Timeline window. This opens the properties window for that item.

3

Toolbox

Comparable to what we know from Visual Studio, the toolbox contains all the tools available. Since Blend is a design tool, tools such as a Pen, Paint Bucket, and so on are available in the toolbox.

4

Assets window

The Assets window contains all controls (assets) that we can drag onto the design surface such as Buttons, ComboBoxes, and so on.

5

Design workspace

This is where all the action takes place! We can drag items from the Toolbox or the Assets window, rearrange them and so on to create a compelling user experience.

6

Properties window

The Properties window allows us to change the properties of the selected item. We can change the color, layout properties, transform properties and so on.

  1. 4. Now that we know our way around the designer, we can get creative. We'll start with MainPage.xaml and split the main Grid (LayoutRoot) into two rows. Instead of writing the XAML code for this, we'll do this in the designer. Click on the icon on the top left of the user control in the designer so that the Grid will be in the Grid layout mode. This can be seen in the following screenshot:

  1. 5. Now, click on the left bar next to the user control to add a row. It's possible to change the height of the created row by dragging the handle. The following screenshot shows a row added to the Grid::

  1. 6. Select the ComboBox in the Assets window. Use the search function in this window to find it more quickly. On the designer, drag to create an instance of the ComboBox and place it on the top row that was just created. This can be seen in the next screenshot:

  1. 7. In the Properties window, give this ComboBox the name HotelComboBox and set the DisplayMemberPath property to Name. In the following screenshot, note that we are making use of the Search functionality within the Properties window. Simply enter part of the name of the property you are looking for (here displ) and Blend will filter the available properties.

  1. 8. With the ComboBox still selected in the Properties window, change to the Events view (top arrow in the next screenshot). In the list of events, double-click on the SelectionChanged event, so Blend will create an event handler (bottom arrow in the next screenshot).

  1. 9. Let's now move back to the Design view of MainPage.xaml. Select the Grid item in the Toolbox. In the bottom cell of the LayoutRoot (the main grid control) drag to create a nested grid. Create four rows and two columns using the same technique as before. Columns are created quite logically by clicking on the top bar of the control. The result is shown in the following screenshot:

  1. 10. With this Grid still selected, change the name to HotelDetailGrid in the Properties window.

  2. 11. In each of the cells, drag a TextBlock from the ToolBox. For the TextBlock controls in the first column, change the Text property as shown in the following screenshot. Don't change the Text property of the controls in the second column; we'll look at these in the coming steps.

  1. 12. Let's now change the background color of the LayoutRoot grid. To do this, select the LayoutRoot node in the Objects and Timeline window and in the Properties window, change the background by selecting a different color in the editor.

  2. 13. InChapter 2, we'll look at how we can make data binding in Blend easier. As of now, we'll just type the XAML code from Blend. In the top-right corner of the Design Surface, select either the Split view or the XAML view. Blend shows us the XAML code that it created in the background. Search for the TextBlock controls in the second column of the HotelDetailGrid and change it as shown in the following code. Note that the generated code might not always be exactly the same as values such as Margin could be different.

    <TextBlock
    Margin="49,8,40,8"
    Grid.Column="1"
    Text="{Binding Name}"
    TextWrapping="Wrap"/>
    <TextBlock
    Margin="49,8,40,8"
    Grid.Column="1"
    Grid.Row="1"
    Text="{Binding Location}"
    TextWrapping="Wrap"/>
    <TextBlock
    Margin="49,8,40,8"
    Grid.Column="1"
    Grid.Row="2"
    Text="{Binding Country}"
    TextWrapping="Wrap"/>
    <TextBlock
    Margin="49,8,40,8"
    Grid.Column="1"
    Grid.Row="3"
    Text="{Binding Price}"
    TextWrapping="Wrap"/>
    
  3. 14. The workflow between Blend and Visual Studio allows us to jump to Visual Studio for the tasks we can't achieve in Blend, for example, adding a WCF service and referencing it in the Silverlight project. In the Projects window, right-click on a file or a project and select Edit in Visual Studio. If Visual Studio is still open, it will reactivate. If not, a new instance will get launched with our solution.

  4. 15. In the website that was created with the project initialization (SilverlightHotelBrowser.Web), we need to have a service that will return the hotel information. A hotel is represented by an instance of the Hotel class as shown in the following code:

    [DataContract]
    public class Hotel
    {
    [DataMember]
    public string Name { get; set; }
    [DataMember]
    public string Location { get; set; }
    [DataMember]
    public string Country { get; set; }
    [DataMember]
    public double Price { get; set; }
    }
    
  5. 16. Of course, we need to add the service as well. To do this, add a Silverlight-enabled WCF service called HotelService. Replace the DoWork sample method with the following code:

    [OperationContract]
    public List<Hotel> GetHotels()
    {
    return new List<Hotel>
    {
    new Hotel
    {
    Name = "Brussels Hotel",
    Price = 100,
    Location = "Brussels",
    Country = "Belgium"
    },
    new Hotel
    {
    Name = "London Hotel",
    Price = 200,
    Location = "London",
    Country = "United Kingdom"
    },
    new Hotel
    {
    Name = "Paris Hotel",
    Price = 150,
    Location = "Paris",
    Country = "France"
    },
    new Hotel
    {
    Name = "New York Hotel",
    Price = 230,
    Location = "New York",
    Country = "USA"
    }
    };
    }
    
  6. 17. Perform a build of the project so that the service is built and is ready to be referenced by the Silverlight application.

  7. 18. In the Silverlight project, add a service reference by right-clicking on the project and selecting Add Service Reference.... Click on the Discover button and the service should be found. Set the namespace to HotelService.

  8. 19. In the MainPage.xaml.cs, add the following code to load the hotels in the ComboBox control:

    public MainPage()
    {
    InitializeComponent();
    HotelService.HotelServiceClient proxy = new SilverlightHotelBrowser.HotelService.HotelServiceClient();
    proxy.GetHotelsCompleted += new EventHandler<SilverlightHotelBrowser.HotelService. GetHotelsCompletedEventArgs>(proxy_GetHotelsCompleted);
    proxy.GetHotelsAsync();
    }
    void proxy_GetHotelsCompleted(object sender, SilverlightHotelBrowser.HotelService. GetHotelsCompletedEventArgs e)
    {
    HotelComboBox.ItemsSource = e.Result;
    }
    
  9. 20. In the SelectionChanged event handler of the ComboBox, add the following code to load the details of a hotel once the user selects a different option:

    private void HotelComboBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
    {
    HotelDetailGrid.DataContext = (sender as ComboBox).SelectedItem as HotelService.Hotel;
    }
    
  10. 21. Build and run the application in Visual Studio.

With these steps completed, we have created the application using both Blend and Visual Studio. For an application as easy as this one, there is less profit in switching between the two environments. However, with larger applications requiring large teams containing both developers and designers, this strong integration can turn out to be very helpful.

How it works...

Visual Studio and Blend integrate nicely with each other. It's easy to jump from one application to the other. This allows a great workflow between designers and developers.

Designers can work in Blend and the changes made in this tool are automatically picked up by Visual Studio, and vice versa. This is achieved through the use of the same files (both code files and project files) by the two tools. A solution created in Blend will open in Visual Studio. The same holds true for a solution created in Visual Studio; Blend can work with it.

See also

InChapter 2, we'll use some more features of Blend. We'll perform data binding directly from Blend.