This post is part of a series of blog posts and videos about the Task-It (task management) application that I have been building with Silverlight 4 and Telerik's RadControls for Silverlight 4. For a full index of these resources, please go here. One of the posts listed in the index provides a full source download for the application, and I will be referring to that source code in this post.
In my last 2 event-related posts I talked about passing information between components via events and using events in providing feedback to a user when time consuming operations are in progress. I also blogged recently about handling data load (and data save) errors. If you have not read those posts yet, I recommend going to the Task-It index (see link above) and reading those posts first. These lay the base for what I am going to talk about in this post.
Trapping the error
We saw in the handling data load errors post how when the LoadOperation is complete (the call to the server asking for data...in this case a list of tasks), we first check to see if it has any errors.
So let's assume that an error has occurred. The first thing that will happen is that we call a utility method that will take in the information passed as parameters and fire and event, the second thing is that we will mark the error as 'handled'. Note that the first argument as a string from a resx (string resource) file. This is so that our error message can be 'internationalized', or 'localized', or whatever you want to call it. Put it this way we can present it in other languages. :-)
Publishing the event
Now let's take a look at the utility method, which lives in the Core folder under the Task-It project.
Yes, it only has two lines of code, but I'd rather have single lines that call this utility method throughout my code, rather than repeating these 2 lines every time.
So we new up an instance of OperationErrorArgs, passing our error information, and then fire the event using the same Messenger class used in the last two event posts. OperationErrorArgs is very simple. It simply stores the error strings in two properties.
Listening for the event
So now that we have fired the event, let's take a look at where we listen for it. Just a reminder that what makes this event unique from the many other events in the app is the argument object that is being passed. So to listen, I have the following line in the constructor of MainPageViewModel, in the ViewModels folder under the Task-It project.
Now let's look at the method that is called when the event is fired, OnOperationError.
The first line calls a utility method that basically just joins the 'user friendly' message we pulled from our resx file with the error message that came back from the LoadOperation. The result is stored in a property in the view model called ErrorMessage. Then we call NotifyError, which simply fires the Error event that notifies our user control that an error has occurred.
In the code-behind of MainPage.xaml, in the Task-It project, we naturally listen for that event and call the OnError method.
This method simply shows our RadWindow that lives in the XAML.
Lastly, let's look at the RadWindow in MainPage.xaml.
Notice that it has a TextBox that is bound to the ErrorMessage property in our view model.
So we saw that when an error occurs, we use a utility method to send an event with an argument object that contains our 'user-friendly' error message. The view model for our main page listens for the event, stores the error message in a member variable, sends an event to MainPage to inform it that an error occurred, and the listener for that event displays the ErrorWindow.
Subscribe to be the first to get our expert-written articles and tutorials for developers!