In this article I’m going to introduce „WinRT XAML Validation“, a library that brings consistent validation of user input to WinRT/C#/XAML apps. Upcoming blog posts will cover several aspects of the library in depth.
I like WinRT. I like its approach of bringing common functionality to very different languages. But WinRT is not perfect. At least in „version 1“ (shipped with Windows 8 ) it has several shortcomings in various parts.
One of those shortcomings is user input validation in C#/XAML Windows Store Apps. Of course there are Data Annotations in .NET 4.5 for WinRT and the
ValidationAttribute, also the
INotifyDataErrorInfo interface is available. But what’s missing is the whole UI stuff. There are no UI controls or attributes on existing UI controls to show errors for validated properties. Hence at the moment you’re forced to execute validation manually and react accordingly to update the UI state.
„WinRT XAML Validation“ library
I’ve been unsatisfied with this lack of support for complete user input validation in WinRT/XAML. Thus a colleague and I developed a library, that fills the missing pieces and goes beyond trivial validation tasks. We needed comfortable and extended validation functionality for one of our development projects and now we want to make the resulting bits available as easy-to-use library.
WinRT XAML Validation has a project page on Codeplex and is free-to-use under the Ms-PL license.
>> Codeplex Project Page <<
The WinRT XAML Validation library is aimed to give developers a consistent, flexible and easy-to-use library to perform user input validation in WinRT/XAML apps. Developers should get a generic approach, that guides them through all steps of validating user input and performs the work for them.
With the WinRT XAML Validation library, you get a whole bunch of functionality, including:
- Validation Attributes: Define custom validation logic with Data Annotations on your model entities. The library contains extended base attributes for validation with support of warnings/errors, async validation and validation logic, that spans more than one property.
- Implicit Validation Execution: Use the
ValidationBindableBase class on your model entities to perform validation. You can opt-in to let validation execute automatically when a entity property changes (implicit validation).
- Explicit Validation Execution: Instead of running validation everytime a property changes on your model entities, you can call the
ValidationBindableBase.ValidateAsync() method explicitly to validate the defined validation attributes.
- Manual Validation: Besides the use of validation attributes to define validation logic, you can easily perform custom validation logic and add corresponding validation messages to a Dictionary, using the
ValidationBindableBase.ValidationMessages.Add() method. The provided UI controls will update themselves automatically.
- Validation UI Controls: There are two easy-to-use XAML UI controls to show validation messages. First there is the
ValidationPanel control that wraps a control whose bound property should be validated (for example a
TextBox). If validation fires for the property, the
ValidationPanel will show a red border (orange for warnings) and optionally the validation message. More than one validation message is supported. The second control is the
ValidationSummary, which can show aggregated validation messages for a whole form and its bound entity. Both controls update immediately when validation messages change, even in async scenarios.
I want to give credit to the Patterns and Practices group at Microsoft for developing and releasing the Prism for Windows Runtime library (f.k.a. Kona). It’s an awesome library that adds much value to the development of WinRT apps and I encourage you to check it out if you’re serious about Windows 8 business app development.
Basic ideas and concepts for the WinRT XAML Validation library are borrowed from Prism for WinRT. For example, the
BindableValidator class has been taken over, but includes much more functionality now. While Prism for WinRT comes with some validation bits, WinRT XAML Validation comes with a more complete approach to user input validation.