WinRT XAML Validation library: Getting Started

This little blog posts describes how you can include the WinRT XAML Validation library in your project and get it to work.

Download

First go to the WinRT XAML Validation Codeplex project page and download the latest bits. The download package includes the sources of the library as well as a demo project (Windows Store App), which showcases many of the concepts and features of the library:

 

Usage by Source

To use the WinRT XAML Validation library, you have two possibilities. The first one is to include the library sources. To do this, copy the WinRTXAMLValidation.Library project to your app’s solution folder and add the existing WinRTXAMLValidation.Library.csproj project to your solution.

Add a reference to the library project everywhere you want to use the validation functionality. To get the UI control to work properly, you have to include the ValidationStyles.xaml file from the Styles folder of the library in your project. There are two different ways to do this:

  • Copy the ValidationStyles.xaml file to your Windows Store App project and reference it as ResourceDictionary in your App.xaml.
  • Add the ValidationStyles.xaml file as link in your Windows Store App project and reference it as ResourceDictionary in your App.xaml.

You’re free to choose from these approaches. Go the „copy way“ if you want to adapt the validation styles to your needs. Go the „add as link“ way if you simply want to use the UI controls as they are.

Usage by Assembly

If you don’t want to include the library’s sources to your solution, you can just add the compiled assembly as reference. To do this, first compile the WinRT XAML Validation library as Debug or Release. Copy the output assembly to your solution and reference it in every project where you want to use the validation functionality.

Copy the ValidationStyles.xaml file from the Styles folder of the library to your Windows Store App project and reference it as ResourceDictionary in your App.xaml.

Introducing the WinRT XAML Validation library

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.

Prologue

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 <<

Functionality Overview

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.

Credits

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.