Let's be clear; this is not exactly an issue but this is the way the RIA asynchronous programming works. Consider a case as follows:
It looks simple but surely you will go through a period of frustration if you ignore the remainder of this article.
THE NORMAL WAY OF DOING
Normally once the popup is closed we are reloading the 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 a problem for our previosu 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; use 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();
- }
- /// <summary>
- /// CallBack Function for SubmitChanges
- /// </summary>
- /// <param name="so"></param>
- 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.
- /// <summary>
- /// CallBack Function for SubmitChanges
- /// </summary>
- /// <param name="so"></param>
- 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 is only one choice from the set of such operations; another example I can mention here is the way the collection is getting loaded using LoadOperation by executing the Entity Query.