Silverlight and .NET RIA Services - Step by Step - 3

by rahul 9/22/2010 10:07:00 PM

So far in this series, we have done the following…

Part 1 > Setup database, create entity data model, create a simple domain service, and expose OData end points
Part 2 > Consume OData Endpoints using Internet Explorer [or any other client] for that matter

In this post, initially you will learn how to create new methods in the Domain Service, and consume it in Silverlight using Code.

    Open dsAlbumAndArtist.cs and add a new method called GetSortedAlbums as follows…

image

    Add a new page to the ChinookSample -> Views Folder…

SNAGHTMLc450db6

     Inside your MainPage.xaml, add the highlighted code so that you could navigate to page Albums

<Border x:Name="LinksBorder" Style="{StaticResource LinksBorderStyle}">
    <StackPanel x:Name="LinksStackPanel" Style="{StaticResource LinksStackPanelStyle}">

        <HyperlinkButton x:Name="Link1" Style="{StaticResource LinkStyle}" 
                        NavigateUri="/Home" TargetName="ContentFrame" 
                            Content="{Binding Path=ApplicationStrings.HomePageTitle, 
Source={StaticResource ResourceWrapper}}"/> <Rectangle x:Name="Divider1" Style="{StaticResource DividerStyle}"/> <HyperlinkButton x:Name="Link2" Style="{StaticResource LinkStyle}" NavigateUri="/About" TargetName="ContentFrame" Content="{Binding Path=ApplicationStrings.AboutPageTitle,
Source={StaticResource ResourceWrapper}}"/> <Rectangle x:Name="Divider2" Style="{StaticResource DividerStyle}"/> <HyperlinkButton x:Name="Link3" Style="{StaticResource LinkStyle}" NavigateUri="/Albums" TargetName="ContentFrame" Content="Albums"/> </StackPanel> </Border>

     In the Albums.xaml page, Inside the <Grid x:Name="LayoutRoot"> add the following code…

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*" />
            <ColumnDefinition Width="1*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="50" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Button Content="Albums" Name="btnAlbums" Margin="10" Grid.Column="0"/>
        <Button Content="Sorted Albums" Name="btnSortedAlbums" Margin="10" Grid.Column="1" />
        <sdk:DataGrid Grid.Row="1" Grid.Column="0" Margin="10" Name="gridAlbums">
        </sdk:DataGrid>
        <sdk:DataGrid Grid.Row="1" Grid.Column="1" Margin="10" Name="gridAlbumsSorted">
        </sdk:DataGrid>

     Finally, your code behind for Albums.xaml.cs should like the following…
Notice, that using ChinookSample.Web.Services is added in the Silverlight application and enabled Silverlight to talk to the RIA Services automatically using the context. 
     Also notice that calling a method/service is really very simple and can be accomplished in hardly three lines of code. All the code plumbing is already done for you by RIA services in the background.

using System.Windows;
using System.Windows.Controls;
using ChinookSample.Web.Services;

namespace ChinookSample.Views
{
    public partial class Albums : Page
    {
        public Albums()
        {
            InitializeComponent();
            btnAlbums.Click += new RoutedEventHandler(btnAlbums_Click);
            btnSortedAlbums.Click += new RoutedEventHandler(btnSortedAlbums_Click);
        }

        void btnSortedAlbums_Click(object sender, RoutedEventArgs e)
        {
            dsAlbumAndArtist context = new dsAlbumAndArtist();
            gridAlbumsSorted.ItemsSource = context.Albums;
            context.Load(context.GetSortedAlbumsQuery());

        }

        void btnAlbums_Click(object sender, RoutedEventArgs e)
        {
            dsAlbumAndArtist context = new dsAlbumAndArtist();
            gridAlbums.ItemsSource = context.Albums;
            context.Load(context.GetAlbumsQuery());
        }
    }
}

     Once you run this application, you will see an output as follows…

image
     In the next post, we'll discuss how to achieve the same effect declaratively using XAML. Stay tuned.

Until next time, Wave
Rahul


Quote of the day:
Blame someone else and get on with your life. - Alan Woods


blog comments powered by Disqus

Rahul Soni

Rahul Soni  Twitter

 LinkedIn

 Facebook

 Email me



Vivek Kumbhar

Vivek Kumbhar  Twitter

 LinkedIn

 Facebook

 Email me


Stack Exchange

profile for Vivek at Server Fault, Q&A for system administrators and IT professionals

profile for Rahul Soni at Stack Overflow, Q&A for professional and enthusiast programmers

Calendar

<<  May 2013  >>
MoTuWeThFrSaSu
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

View posts in large calendar

All Items
Sign in

Visit Microsoft's Site

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2013, Rahul Soni

Powered by BlogEngine.NET 1.4.5.0