Welcome Friends , . although it seems like a tabloid heading but the
experience with Silverlight can be troublesome and frustrating if you
ignore the async programming model/calls which used predominantly in
Silverlight and WCF RIA. The Async model changes logic and flow of code
creating confusion until you know about it.
So this post is must if you are a newbie to Silverlight with RIA
services and want to spent a little searching through forums/blog for
some simple but crazy results.
Lets start with a example scenario , a typical issue where the list
box didn't update although we used to re-fetch the entity collection
after adding some entity ,using domainservicecontext.
The Issue and Scenario
lets be clear this is not exactly an issue but this is the way the RIA asynchronous programming works.Consider a case as bellow

Now not so difficult but surely you are going to pass through a frustrating stage if you ignore the rest of article.
The Normal way of doing
Normally once the popup closed we are reloading collection and binding to the list.
- private void btnAddNew_Click(object sender, RoutedEventArgs e)
- {
- //Create a New State Object
- State stateObject = new State();
- //Add to the StateCollection of Domain DataContext
- dataContext.States.Add(stateObject);
- //Create a PopUp Child Window Object
- Views.AddNewState chldWind = new Views.AddNewState();
- //Getting the GridLayout Control of the popUp To Be Shown and assign the Newly Created Object
- ((Grid)chldWind.FindName("chldMainContainer")).DataContext = stateObject;
- //Attach The Todo on Close Event handaler
- chldWind.Closed += new EventHandler(chldWind_Closed);
- chldWind.Show();
- }
- void chldWind_Closed(object sender, EventArgs e)
- {
- ChildWindow chldWind = (ChildWindow)sender;
- if ((chldWind.DialogResult == true))
- {
- dataContext.SubmitChanges();
- }
- else
- dataContext.RejectChanges();
- //Refresh The List Box Data
- //*** Your Code to Load Re Fetch the Data***
- }
But wait . this will not work . This is where you need to be aware of async call.
Understanding Asynchronous operation
"In asynchronous mode an application will continue with
other command or functions while the previously command is still in
execution mode"
Now this creates problem for our above sample , We called dataContext.SubmitChanges(); then
in the next line we are binding the list box item Source .We need to
wait for the async call "submitchanges" to be finished. but how do we
will handle it ?????.. simple using CallBack Functions.
Once the command complete its task the specified call back function is called where we can write our data load logic.
The Actual way of doing
Instead of using SubmitChanges() we will use SubmitChanges Method (Generic Action, Object) and we will invoke the data binding logic with in the call back function.
- void chldWind_Closed(object sender, EventArgs e)
- {
- ChildWindow chldWind = (ChildWindow)sender;
- if ((chldWind.DialogResult == true))
- {
- dataContext.SubmitChanges(OnSubmitCompleted, null);
- }
- else
- dataContext.RejectChanges();
- }
- ///
- /// CallBack Function for SubmitChanges
- ///
- ///
- private void OnSubmitCompleted(SubmitOperation so)
- {
- if (so.HasError)
- {
- MessageBox.Show(string.Format("Submit Failed: {0}", so.Error.Message));
- so.MarkErrorAsHandled();
- }
- //Logic to Bind Data to List Box
- LoadData();
- }
As described above the SubmitChange line goes like this
- dataContext.SubmitChanges(OnSubmitCompleted, null);
While calling the following "OnSubmitCompleted " function once the submit operation is over.
- ///
- /// CallBack Function for SubmitChanges
- ///
- ///
- private void OnSubmitCompleted(SubmitOperation so)
- {
- if (so.HasError)
- {
- MessageBox.Show(string.Format("Submit Failed: {0}", so.Error.Message));
- so.MarkErrorAsHandled();
- }
- //Logic to Bind Data to List Box
- LoadData();
- }
Conclusion
This only one pick from the set of such operation another example i
can mention here is the way the collection is getting loaded using
LoadOperation by executing the Entity Query.
So keep Async Operations in mind while coding with SL and WCF RIA
service , by doing so not only you will save time but also makes her
happy
,
hope my heading does make a sense , now.