This task has a simple purpose of building a windows application that is capable of sending emails, but I must say that the main goal was that of accustoming to how things flow in WPF. The application was build in Visual Studio Codename “Orcas”, targeted for the .NET 3.5 Framework.
I first created a new project in Visual Studio from the WPF Application template, under Windows Project types, having the Framework target set for 3.5. The main window for this project is a XAML file and the root element is <Window>. Be aware not to make confusion between this one and the root element for a XAML in WPF/E which is <Canvas>. The Window tag contains the reference to the code behind file and to the xml namespaces. You can define your own namespaces and other resources that you have to use, set properties values for the main window of the application. Further on, in a Grid element you can place all the controls that you need. Since the application is an “emailer” it will basically need some textboxes with labels, a checkbox indicating if the message content is plain text or HTML, and a Send button that gets the job done.
Sending emails with WPF is pretty much the same like in .NET Framework 2.0. I used the System.Net.Mail namespace to have access to objects like MailMessage, MailAddress and SmtpClient. Let’s take a brief look at the function that actually sends the email. This function takes the receiver’s email address, the sender’s address, the mail subject and the content of the message as parameters. There is also a parameter signaling if the message body contains HTML. After preparing the mail message object it is important to correctly create a SMTP client, with a valid host and valid login credentials.
Having the input information filled in by a user, it automatically means that data validation must be implemented. This part can raise some issues and because there aren’t any validators included in the ToolBox, we must find some effective and proper validation methods. A great article on WPF validation can be found at http://www.codeproject.com/WPF/wpfvalidation.asp and I strongly suggest taking the example offered there for a better understanding.
Regarding my email application, all the fields are required, plus the email addresses must be valid. If you follow the example from the link above, you can see that the content of a TextBox can be validated in a simple way by: declaring a public property in the code behind file, binding the text property to that specific property and, if an exception is thrown, applying an error template to the TextBox control. But this solution is not flexible enough. Here’s why: I wanted the validation “bounded” to the Send button Click Event, so the method shouldn’t do anything with invalid information filled in. But, the UpdateSourceTrigger associated with the Binding Path of each TextBox (in this case with the value “PropertyChanged”) doesn’t have a value to trigger the validation in any case. So, unless text was typed in the TextBox, the validation would not take place.
The solution came to me from the same article on WPF validation, written by Paul Stovell. A very good way of handling errors from all the controls in a form and controlling in a better way the validation process is to use an ErrorProvider. This type of control, well known in .NET 2.0, does not exist in WPF, and we have to create one of our own. The example provided with the article was the guide I needed to create a custom ErrorProvider and match it with my requirements. The ErrorProvider calls a validation as soon as the form is loaded, so any breaking of a validation rule is displayed right away. The validation function recursively finds all the bindings on the current data context. Any changes occurred in the bound properties are followed by a search of the respective IDataErrorInfo implementation and if it’s found, the corresponding error message is displayed using the static Validation class (that means the existing error template is applied to the TextBox). In another class (that implements IDataErrorInfo), let’s say ValidationClass or MyValidation, I have declared all the properties to which the Text of the TextBox controls bound. I have also used an email validation function, and specified the custom error messages that would occur with the validation exception generated by invalid data. These error messages would be set as tooltips for the controls with the invalid data using a Style element (our template defined in the <Application.Resources> section of the App.xaml file).
Having these classes defined, after the calling of InitializeComponent() method, I had to instantiate a new object of the validation class and set the data context of the application window to that object. Also, for a better user interaction, the content of the textboxes becomes highlighted when they gain focus (by mouse click or by Tab succession) – this wasn’t absolutely necessary, but I thought it might be a nice thing to have done in WPF and also resembling to the way some textboxes behave in Expression Blend.
This is it. The validation is complete and this small task has surely given me some perspectives on how things can be handled in WPF (I’m talking mainly about the validation and WPF error handling part, because this was the tricky issue in accomplishing the task). I would like to emphasize the advantage offered by the presence of an ErrorProvider. If we have an application with a rich domain model, the ErrorProvider remains a front row solution.
Currently rated 1.0 by 2 people
- Currently 1/5 Stars.
- 1
- 2
- 3
- 4
- 5