Chapter 6. Working with RESTful Data Using Silverlight

This is the penultimate chapter of this book in which we will explore how we can consume WCF 4.5 RIA and RESTful services using Silverlight 5.

In this chapter, we will cover the following points:

  • Introduction to Silverlight 5
    • New features in Silverlight 5
  • Understanding WCF 4.5 RIA Services
  • Implementing a sample application that uses WCF 4.5 RIA Services
  • Consuming the WCF 4.5 RIA service using a Silverlight 5 client

Introducing Silverlight 5

Silverlight (codenamed Windows Presentation Foundation/Everywhere or WPF/E) is a browser plugin. Silverlight is a client-side technology that provides support for RIA. It can be used to enhance the look and feel of web-based applications. The new versions of Silverlight contain enhanced features for building RIA-based business applications and rich media applications. Cross-browser and cross-platform compatibility and awesome support for rich graphics and animation are some of the striking features of Silverlight. Also, it runs in a sandbox environment—a subset of the WPF Framework.

The newer versions of Silverlight provide great support for Printing (including virtual print preview), COM Automation, Web cam and microphone, MEF (managed extensibility framework), and WCF 4.5 RIA Services. On a different note, the performance of Silverlight 5 applications has been optimized to a great extent.

WCF 4.5 RIA Services provide a framework that helps you to connect .NET client objects with .NET server objects using WCF 4.5. The newer version of Silverlight provides support for WCF 4.5 RIA Services. Note that WCF 4.5 RIA Services (formerly known as .NET RIA services) exposes data in an optimized .NET Binary format or ATOM, JSON, or in an OData format to the Silverlight application. WCF 4.5 RIA Services simplifies the development of RIA applications, that is, applications designed using technologies like Silverlight 5. RIA services provides you with a framework that eliminates the need of duplicating your middle-tier components.

Silverlight provides excellent support for developing the next generation of cross-browser and cross-platform Rich Internet Applications (RIAs). It facilitates the design and development of engaging, interactive user experiences for web and mobile applications. However, Silverlight 5 looks like the last release of Silverlight from Microsoft, and HTML 5 will be the choice going forward.

The choice between opting for Silverlight 5 or HTML 5 depends on many factors. If you are creating a Line of Business Application (LOB), you can choose Silverlight 5 and leverage its data binding features. On the contrary, if you need applications where images, links and textboxes, animations, and interactivity is needed, HTML 5 is a better choice. However, Silverlight 5 is better suited for intranet applications rather than web-based applications. Also, note that Silverlight 5 is only supported on Windows Phones.

New features in Silverlight 5

The following are the new features added to Silverlight 4 and 5:

  • Enhancements to controls: The new features added to the controls in Silverlight 5 include: text overflow, support for multi-click, type-ahead text search, incorporation of the DataContextChanged event, text tracking, improvements in text rendering, layout, clarity, and so on. In Silverlight 5, you can set the default filename when using the SaveFileDialog box using the DefaultFileName property. Here is how you can use it:
    SaveFileDialog fileDialog = new SaveFileDialog();
    fileDialog.DefaultFileName = "Demo.txt";
    fileDialog.ShowDialog();  
  • Improvements to data binding.
  • XAML changes: In the XAML stack, the features that have been added in Silverlight 5 include: XAML debugging, markup extensions, implicit data templates, binding in styles, and so on. Extensible Application Markup Language or XAML is an XML-based declarative markup language from Microsoft that enables you to design stunning user interfaces for your WPF or Silverlight applications. The XAML content is stored in an .xaml file, and you can use XAML to separate the user interface definition of your applications from the runtime logic by using code-behind files.

    Note

    Note that XAML debugging works only in Internet Explorer 9 and above.

  • Implicit data template is a great new feature that enables you to set a data type using the DataType property to the data template, instead of attaching the data template to every control in your page. Here's an example of a code snippet that illustrates this:
    <ListBox x:Name="users">
    <ListBox.ItemTemplate>
    <DataTemplate>
    <StackPanel>
    <TextBlock Text="{Binding FirstName}" FontWeight="Bold" />
    <TextBlock Text="{Binding LastName}" />
    </StackPanel>
    </DataTemplate>
    </ListBox.ItemTemplate>
    </ListBox>

    Mark-up extensions is a feature that enables you to execute code at XAML parsing time . These include {Binding}, {StaticResource}, {RelativeSource}, and so on. You can also create your own custom markup extensions.

  • Updates/changes to user interface, graphics, and media.
  • Support for Webcam and Microphone: Silverlight 4 and Silverlight 5 enables you to build applications that have the capability of sharing video and audio.
  • Optimized performance: Applications built using Silverlight 4 and Silverlight 5 are amazingly fast compared to the earlier versions of Silverlight. Also, Silverlight 5 applications start much quicker. Silverlight 5 provides support for 64-bit operating systems and allows trusted applications to access the local file system without any restriction.
  • Enhanced Support for COM Interoperability: Silverlight 4 and Silverlight 5 can interoperate with COM interfaces. The following code snippet shows how to communicate with Microsoft Office applications from Silverlight:
            dynamic excelObject = ComAutomationFactory.CreateObject("Excel.Application");
            excelObject.Visible = true;
            dynamic workbookObject = excelObject.workbooks;
            workbookObject.Add();
            dynamic sheetObject = excelObject.ActiveSheet;
  • To make your Silverlight 4 and Silverlight 5 applications communicate with Microsoft Word, you can write the following piece of code:
            dynamic wordObject = ComAutomationFactory.CreateObject("Word.Application");
            wordObject.Documents.Add();
            wordObject.Visible = true;
  • Improved support for RIA services: With Silverlight 5, support for WCF 4.5 RIA Services has been improved. You now have support for complex types, better MVVM support, and also support for much better customization of the generated code.
  • Enhanced support for out-of-browser applications: Silverlight 4 and Silverlight 5 provides support for RIA applications, and this support is provided without you having to download and install any additional code or runtime. You now have support for cross-domain network access, accessing user's folders, COM Interop, and HTML hosting.
  • Note that you include the following configuration in the ApplicationManifest.xaml file, so that you can leverage the elevated permissions while installing and using out-of-browser applications.
    <OutOfBrowserSettings.SecuritySettings>
    <SecuritySettings ElevatedPermissions="Required" />
    </OutOfBrowserSettings.SecuritySettings>
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset