Progressbar control in Silverlight 3.0

In this article, I will explain couple of things step by step. First how to work with Progressbar control of Silver Light 3.0.Then how to consume WCF service in Silverlight application when both are in same solution.

How to consume WCF service in Silverlight application when both are in same solution.

Step 1

Create a Web Application. Give any name of your choice. I am giving name here TestingProgressBar.

Step 2

Right click on Web Application project and Add new Item. Go to Silverlight category and choose Silverlight-enabled WCF Service project template. Give any name of your choice. I am leaving the defualt name Service1.svc

Step 3

Write the service. Service is returning a List of Authors. Author is class with properties Name and Article. Service method having very simple functionality, where a list of Author is getting construed and returned.

Build the web application project. After successfully compilation right click on service and view in browser to check service is running successfully or not.

You should get the below output in browser. If you getting below output, it means you have successfully created the service and it is running. If you are not getting below output again cross check the previous said steps.

In above XAML, drag and drop one DataGrid and Progress Bar just below Button control. To open the tool box, from Menu select View and Tool Box. After dragging and drop modify the XAML for DataGrid and ProgressBar as below. <data:DataGrid x:Name="myGrid" AlternatingRowBackground="Aqua" Grid.Row="1" Visibility="Collapsed"/><ProgressBar x:Name="myProgressbar" Width="400" Height="100" Grid.Row="1" Visibility="Collapsed"/>

Step 7

Add the service reference. To add right click on reference in Silverlight application and select add Service Reference. From window click on Discover, this will discover the service in the solution. Give any name for the service reference; I am leaving the default name.

On Button click event, I am creating a proxy of service and just handling the Completed event and calling the Async method. In other work, I am just hiding and displaying the progress bar and grid view. After calling the Async method I am making IsWorking property true. privatevoid myButton_Click(object sender, RoutedEventArgs e)

On completed event, I am assigning item source of datagrid as e.reult. In other work just making IsWorking property as false. And Hiding the progress bar and displaying the datagrid. void proxy_DisplayAuthorCompleted(object sender, DisplayAuthorCompletedEventArgs e)

{

this.IsWorking = false;

myProgressbar.Visibility = Visibility.Collapsed;

myGrid.Visibility = Visibility.Visible;

myGrid.ItemsSource = e.Result;

}

Note for cross Domain issue

Since in our sample, both Silverlight application and WCF service is in same solution then there is no cross domain issue will come. But even if you are getting cross domain issue make sure of below things

Make Startup project to Web Application Project. Right Click on Web Application Project and make it as startup project.

Make SilverLightApplication1.aspx page as start up page.

Output

On clicking of button, service will get called and progress bar will show the waiting. After that Datagrid will get populated with the data.

Complete code is available in the source code attached with this article.

Conclusion

In this article, I explained how to consume WCF service in Silverlight application when both are in same solution. I also explained how to work with Progressbar control in Silverlight 3.0. Thanks for reading.