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

by rahul 9/28/2010 3:24:00 PM

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
Part 3 > Consume entities exposed using RIA services with Silverlight [code behind]
Part 4 > Consume entities exposed using RIA services with Silverlight [declaratively using XAML]

In this part, let's fix the issue of displaying a status while the data is loading. What I want next, is to show some text "Loading…" while the data is being loaded. Once data is loaded the status message should disappear from screen. You can do this through code behind by polling and changing visible status, but that is boring and inefficient. That's because, you will write this piece of code everywhere. Let's do something more interesting and easy.

Just for testing let's add a textblock like this…

        <TextBlock Height="49" Name="textBlock1" 
                   VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="32" 
                   Text="{Binding ElementName=source, Path=IsLoadingData, Mode=TwoWay}" />

You'll find that False would appear in your design pane. What about binding the Visibility property instead of Text? That wouldn't work, because Visibility property as you can see below take Collapsed and Visible.

image

As I mentioned earlier, you could handle this issue easily in code behind, but that won't be usable across and usability would hamper because even you write a method you will need to call that method in code behind. This is a good place to use a value Converter.

    Add a new class called Converters.cs inside Helpers folder.

image

    Copy paste the code below…

using System;
using System.Windows;
using System.Windows.Data;

namespace ChinookSample.Helpers
{
    public class BooleanToVisibility : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter,
                                System.Globalization.CultureInfo culture)
        {
            return ((bool)value == true ? Visibility.Visible : Visibility.Collapsed);
        }

        public object ConvertBack(object value, Type targetType, object parameter,
                                System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

    As you can see, the code above just implements IValueConverter interface, and it returns Visible and Collapsed
based on the input it receives.
    Okay, so our converter is ready. Let's put it in action [you can do this across your project]. The Albums.xaml page looks as follows…

<navigation:Page x:Class="ChinookSample.Views.Albums" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ria="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.DomainServices"
xmlns:service="clr-namespace:ChinookSample.Web.Services"
xmlns:helper="clr-namespace:ChinookSample.Helpers"
mc:Ignorable="d"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
d:DesignWidth="640" d:DesignHeight="480"
Title="Albums Page" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
    <Grid x:Name="LayoutRoot">
        <Grid.Resources>
            <helper:BooleanToVisibility x:Key="BoolToVisibility" />
        </Grid.Resources>
        <ria:DomainDataSource Name="source" AutoLoad="True" LoadSize="50" 
QueryName="GetSortedAlbums"> <ria:DomainDataSource.DomainContext> <service:dsAlbumAndArtist /> </ria:DomainDataSource.DomainContext> </ria:DomainDataSource> <sdk:DataGrid Margin="10" Name="gridAlbums" ItemsSource="{Binding ElementName=source, Path=Data}"> </sdk:DataGrid> <TextBlock Height="49" Name="textBlock1" Text="Loading..." VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="32" Visibility="{Binding ElementName=source, Path=IsLoadingData, Mode=TwoWay, Converter={StaticResource BoolToVisibility}}" /> </Grid> </navigation:Page>

    The changes from the previous post are highlighted
    Notice > This namespace is imported so that you could refer to the converter in XAML
    Notice > Define a Key BoolToVisibility that would be used later while binding.
    Notice > A text block that shows the message, and has its visibility property bound to the
DomainDataSource -> source, and has a converter that changes visibility to Visibile/Collapsed if the value is true/false respectively.
    Also notice LoadSize is set to 50. This will ensure that the data is loaded in chunks of 50. Hence, the Loading… status should appear and disappear every time the data is loaded. Remove this attribute, and see how Loading… disappears once done. Then put this attribute back and see how Loading… switches between visible/invisible every time it goes to fetch the data.
    In this next post, you will find out how to add paging functionality. Stay tuned!

Until next time, Wave
Rahul


Quote of the day:
Always forgive your enemies; nothing annoys them so much. - Oscar Wilde



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

<<  June 2013  >>
MoTuWeThFrSaSu
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

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