Simple Silverlight 2 Progress Bar for Web Services

    Data Services With Silverlight 2 - Amazon cover.- SmallHere is a small/basic but useful sample to create a progress bar in XAML that is updated when making a call to a RESTful web service using WebClient (in my case) for Silverlight from my book.  The code can easily be put inside of a self contained Silverlight user control of its own, which would allow it to easily be used in multiple areas of the application. I’ll leave that part up to you :)

    The following XAML creates an outer Border panel and an inner Border panel. The outer Border sets the boundaries of the horizontal progress bar. Set the width of the outer Border (named brdProgressBoundary) to whatever width you want. The bar will grows until it reaches this point. The inner Border (named brdProgressMeter) starts with a width of 0 and is incremented using the percentage from the web service’s DownloadProgressChanged event handler to increase its width until it reaches 100% (which is where I has the same width as the outer border). I used a LinearGradientBrush to create a slight color effect in the progress bar. You could add whatever type of content you want here.

    <Border x:Name="brdProgressBoundary" CornerRadius="5" Width="250" Height="25" 
            BorderThickness="1" BorderBrush="#FF00135B" Margin="10,5,10,10">
        <Grid HorizontalAlignment="Stretch">                            
            <Border x:Name="brdProgressMeter" Opacity="0.6" CornerRadius="5" 
                HorizontalAlignment="Left" Width="250">
                <Border.Background>
                    <LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5">
                        <GradientStop Color="#FEBDE2F4" Offset="0.0"/>
                        <GradientStop Color="#FF00135B" Offset="0.75"/>
                    </LinearGradientBrush>
                </Border.Background>                                                                                                                                            
            </Border>
            <TextBlock x:Name="lbProgressValue" HorizontalAlignment="Center" 
                VerticalAlignment="Center" FontSize="10"/>
        </Grid>
    </Border>

    The key to moving the progress bar is knowing when the progress has changed and by how much. When a call is made through WebClient to a remote web service, if an event handler is added to the DownloadProgressChanged event, the handler receives notification when the progress has changed. For example, the following code will increment the inner border’s width and display a the percentage complete in a TextBlock inside of the inner border. This handler will execute at intervals while the WebClient’s web request is moving along.

    C#

    private void svc_DownloadProgressChanged(object sender, 
            DownloadProgressChangedEventArgs e)
    {
        int pct = e.ProgressPercentage;
        lbProgressValue.Text = pct.ToString() + "%";
        brdProgressMeter.Width = pct * 0.01 * brdProgressBoundary.Width;
    }

    VB

    Private Sub svc_DownloadProgressChanged(ByVal sender As Object, _         ByVal e As DownloadProgressChangedEventArgs)
        Dim pct As Integer = e.ProgressPercentage
        lbProgressValue.Text = pct.ToString() & "%"
        brdProgressMeter.Width = pct * 0.01 * brdProgressBoundary.Width
    End Sub

    As you might imagine, you’ll need to initialize the state of the progress bar before each WebClient’s async call and you will need to hide the progress bar when it is no longer needed.  Here is the Progress as it looks in process:

    image

    Enjoy!

    DotNetKicks Image
    #1 Steve on 8.31.2008 at 7:42 PM

    John, Going back a couple of days. Your datapoints columns in MSDN I went to the MSDN site and wanted to download the code but I keep getting an old demo not your current ones code from the link. In fact it looks like an old demo Shawn wildermuth wrote in July and looking at the file properties they are also July. I cleared my IE cache and still same files come down. The files inside the exe that comes down. This could be just something messy on my pc as this is a test rig but I was wondering if you wouldn't mind checking it for me as it has me baffaled. Ta



    #2 Post: 177 on 9.01.2008 at 9:53 AM

    Post: Approved at: Sep-1-2008 Simple SL2 Progress Bar for Web Services &quot;Here is a small/basic but



    #3 John Papa on 9.01.2008 at 10:14 AM

    Steve ... Which MSDN article/issue are you referring to so I can take a look? (i.e. July 2008, Septemeber 2008)



    #4 Dew Drop - September 1, 2008 | Alvin Ashcraft's Morning Dew on 9.01.2008 at 10:23 AM

    Pingback from Dew Drop - September 1, 2008 | Alvin Ashcraft's Morning Dew



    #5 Simple Silverlight 2 Progress Bar for Web Services « Rich Internet Applications on 9.01.2008 at 1:20 PM

    Pingback from Simple Silverlight 2 Progress Bar for Web Services « Rich Internet Applications



    #6 Steve on 9.01.2008 at 6:59 PM

    John the MSDN article is Service Driven Apps with WCF and Silverlight 2 article the download is SLDataServices2008_09a.exe but i have tried 2 pc's now and get old code on both.

    thanks



    #7 Steve on 9.01.2008 at 7:02 PM

    John an update. if you google SLDataServices2008_09a.exe which is what your article has a link to it actually googles back with an article from Shawn - Service-Driven Apps With Silverlight 2 And WCF

    thanks again



    #8 John Papa on 9.01.2008 at 8:13 PM

    Thanks Steve. I contacted MSDN Magazine about the mixup. In the meantime, you can grab the code from this post.

    johnpapa.net/.../download-code-f



    #9 Silverlight news for September 2, 2008 on 9.02.2008 at 5:24 AM

    Pingback from Silverlight news for September 2, 2008



    #10 User links about "width" on iLinkShare on 9.13.2008 at 2:18 PM

    Pingback from User links about "width" on iLinkShare



    #11 gfhh on 4.07.2009 at 12:11 AM

    fghh



    #12 Justin Toth on 6.20.2009 at 11:57 PM

    Hi John,

    Any idea how you could implement this if you're manually consuming WCF services instead of using the ugly service reference code (no DownloadProgressChanged event)? I'm using David Betz' approach, found here:

    www.netfxharmonics.com/.../Understanding-W



    Leave a Comment