In my last blog post I had explained the two standard ways of  validating data in Silverlight 4. One way was implementing “INotifyProprtyChanged” interface. And the second way was “INotifyDataErrorInfo” interface, which is the new option available with Silverlight 4 for validating data.

Now in this blog post I will show, how we can use the validation asynchronously with “INotifyDataErrorInfo” interface in Silverlight 4.

Simple Scenario where Asynchronous Validation can be useful:

Assume that we are just registering new users for a website accepting username and password in Silverlight front-end UI. Now when user enters the preffered username, we need to check that preffered username is available or not. So we just call the web service for checking the availability of preffered username. But as we are using Asynchronous Data Validation to perform this username validation, and so while the web service returns the response, Silverlight front-end UI still remains available for User for entering password and so on……….

Here is the Entity class “User” which implements the “INotifyDataErrorInfo” interface as shown in the image below……

Here is the property “username” in the entity class “User” which is getting validated asynchronously…….

In this code “WebService.CheckUserNameAvailability(string userName, UserNameCallBack callBackMethod)” accepts userName as first parameter and CallBackMethod for UserNameCallBack delegate as a second parameter. So Web Service returns the response by calling this callBackDalegateMethod and passing “isUserNameAvailable” variable in the method. Then this method process the validation as shown in the image below………

So this callBackDalegateMethod will be called whenever the WebService will be ready with the response. So if web service returns the response in say 10 seconds, by the time, User can enter the password and so on.. And we can keep the “OK” button disable by that time as User can not click it before validation completes on “UserName” as shown in the image below…..

After this asynchronous validation, if UserName will not be available silverlight validation framework generates the error visualisation as shown in the image below……

 

So it was all about Asynchronous Data Validation with Silverlight 4.

For more info on silverlight, just go to, http://www.silverlight.net

For more on silverlight 4, click here, http://www.silverlight.net/getstarted/silverlight-4/

For more details on Silverlight 4.0, you can also browse through following blogs………

http://timheuer.com/blog/          —  Tim Heuer Blog – Silverlight Program Manager

http://blogs.silverlight.net/blogs/jesseliberty/       — Jesse Liberty Blog – Silverlight Program Manager

http://johnpapa.net/              —  John Papa website

http://adamkinney.wordpress.com/          — Adam Kinney Blog — For Silverlight graphics, interface and Expression Blend