XAML Playground
about XAML and other Amenities

Silverlight 5.0: Custom Markup Extensions and Roles

2011-04-23T00:54:32+01:00 by codeblock

Starting from Silverlight 5.0 you can create custom Markup Extensions and this is an interesting feature to easily encapsulate some logic and make it easy to be applied to properties in the XAML markup. Until now you could only use a few extensions to apply resources (StaticResource), make databinding (Binding) and connect properties with parts of a template (TemplateBinding) but now, implementing a really simple interface you can build your own.

   1: public interface IMarkupExtension<out T> where T: class
   2: {
   3:     T ProvideValue(IServiceProvider serviceProvider);
   4: }

Using this interface you can specify the type to which the markup extension can be applied (the generic type T) but if you do not need a control about this type you can extend the MarkupExtension abstract class that is like you are extending IMarkupExtension<object>.

Inside the ProvideValue method there is the whole logic of the extension and using the IServiceProvider passed by the runtime you can get access to three services that let you get some informations about the markup where the extension is located.

IRootObjectProvider: provide a reference to the Root object of the VisualTree which the element is part of

IXamlTypeResolver : is able to resolve the name of the tags in the markup to the corresponding type.

IProvideValueTarget : gets a reference to the property and the elements which the markup extension is assigned

To retrieve an instance of this services you can use the GetService method on the IServiceProvider instance.

IProvideValueTarget target = (IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget));

In this example I wrote a RoleBinding extension that a developer can use to connect parts of the User Interface with a Validator class that is able to authorize the access to these parts using a set of custom rules. Let start writing the markup extension:

   1: using System;
   2: using System.Net;
   3: using System.Windows;
   4: using System.Windows.Controls;
   5: using System.Windows.Documents;
   6: using System.Windows.Ink;
   7: using System.Windows.Input;
   8: using System.Windows.Media;
   9: using System.Windows.Media.Animation;
  10: using System.Windows.Shapes;
  11: using System.Windows.Markup;
  12: using System.Security.Principal;
  13: using System.Windows.Data;
  14: using System.Xaml;
  15: using System.ComponentModel;
  16: using System.Diagnostics;
  17: using System.Security;
  18: using System.Reflection;
  19: using System.Globalization;
  20:  
  21: namespace SLPG.MarkupExtensions
  22: {
  23:     public class RoleBindingExtension : MarkupExtension
  24:     {
  25:         /// <summary>
  26:         /// Gets or sets the name of the group.
  27:         /// </summary>
  28:         /// <value>
  29:         /// The name of the group.
  30:         /// </value>
  31:         public string GroupName { get; set; }
  32:         /// <summary>
  33:         /// Gets or sets the name of the feature.
  34:         /// </summary>
  35:         /// <value>
  36:         /// The name of the feature.
  37:         /// </value>
  38:         public string FeatureName { get; set; }
  39:         /// <summary>
  40:         /// Gets or sets the converter.
  41:         /// </summary>
  42:         /// <value>
  43:         /// The converter.
  44:         /// </value>
  45:         public IValueConverter Converter { get; set; }
  46:         /// <summary>
  47:         /// Gets or sets the converter parameter.
  48:         /// </summary>
  49:         /// <value>
  50:         /// The converter parameter.
  51:         /// </value>
  52:         public object ConverterParameter { get; set; }
  53:  
  54:         /// <summary>
  55:         /// Provides the value.
  56:         /// </summary>
  57:         /// <param name="serviceProvider">The service provider.</param>
  58:         /// <returns></returns>
  59:         public override object ProvideValue(IServiceProvider serviceProvider)
  60:         {
  61:             IProvideValueTarget target = (IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget));
  62:  
  63:             bool isAuthorized = false;
  64:  
  65:             if (RoleManager.Current != null)
  66:                 isAuthorized = RoleManager.Current.Validator.Authorize(this.GroupName, this.FeatureName);
  67:             else
  68:                 isAuthorized = true;
  69:  
  70:             if (this.Converter != null)
  71:                 return this.MapToType(isAuthorized, target, this.Converter, this.ConverterParameter);
  72:  
  73:             return this.MapToType(isAuthorized, target);
  74:         }
  75:  
  76:         /// <summary>
  77:         /// Maps to type.
  78:         /// </summary>
  79:         /// <param name="isAuthorized">if set to <c>true</c> [is authorized].</param>
  80:         /// <param name="target">The target.</param>
  81:         /// <param name="converter">The converter.</param>
  82:         /// <param name="converterParameter">The converter parameter.</param>
  83:         /// <returns></returns>
  84:         private object MapToType(bool isAuthorized, IProvideValueTarget target, IValueConverter converter, object converterParameter)
  85:         {
  86:             PropertyInfo info = target.TargetProperty as PropertyInfo;
  87:  
  88:             if (info != null)
  89:                 return converter.Convert(isAuthorized, info.PropertyType, converterParameter, CultureInfo.CurrentCulture);
  90:  
  91:             return isAuthorized;
  92:         }
  93:  
  94:         /// <summary>
  95:         /// Maps to type.
  96:         /// </summary>
  97:         /// <param name="isAuthorized">if set to <c>true</c> [is authorized].</param>
  98:         /// <param name="target">The target.</param>
  99:         /// <returns></returns>
 100:         private object MapToType(bool isAuthorized, IProvideValueTarget target)
 101:         {
 102:             PropertyInfo info = target.TargetProperty as PropertyInfo;
 103:  
 104:             if (info != null)
 105:             {
 106:                 if (info.PropertyType == typeof(Visibility))
 107:                     return isAuthorized ? Visibility.Visible : Visibility.Collapsed;
 108:             }
 109:  
 110:             return isAuthorized;
 111:         }
 112:     }
 113: }

First of all I extend the MarkupExtension class. The core of the extension is the ProvideValue method. Inside this method I get a service that implements the IProvideValueTarget interface to retrieve informations about the property where the extension is applied to. In my case I need to know the type of the property because I have to map the boolean information (true means authorized, false means not authorized) to the property. For example if the target property is Visibility I map the true to Visible and false to Collapsed.

A markup extension can have a number of parameters. They are public properties exposed by the class. Differently from other existing extensions the custom extensions cannot have unnamed parameters. This is a choice of the Silverlight team I hope will change in future releases just to make extensions very close to the WPF ones. In my case the extension support these parameters:

GroupName and FeatureName: two strings that are used to select the feature and the category of feature the element is part. You can specify for example "Products" as GroupName and "Create" as feature. The values and their meaning are completely up to you. By default they are interpreted as role names where the two parts are joined by a dot: GroupName.FeatureName.

Converter and ConverterParameter: like the Binding extension it is a class that implements IValueConverter and a parameter passed to the class during the conversion. It is used to apply custom conversion to the boolean value that comes from the Validator. In the code provided with the post I use a converter to map the boolean to a Color (Red or Green). 

The extension rely on a Lifetime Object that contains the roles granted to the current user. In my example these roles are passed using the InitParams but please do not repeat this in a production environment because a malicious user can easily impersonate different roles. Probably the better thing  is to use an AuthorizationDomainService provided by the WCF RIA Services.

Once you add the RoleManager service to the App.xaml you can also specify a custom validator that will get the authorization requests and can apply every type of logic you need to perform the authorization. This class can be easily created by implementing the IRoleManagerValidator interface.

You can download the code of this article using the following link. I appreciate every suggestion to improve this extension.

Download: SLPG.MarkupExtensions.zip (30KB)

Categories:   News
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed