Thursday, January 12, 2012

Screen Readers with Silverlight Applications

Screen Readers with Silverlight Applications.


Web based applications fly across global boundaries due to which accessibility and ease of use becomes one of the most important factor that drives the sales count of the service we provide.


As per 508 Compliance if we require our service served to audience with disabilities it should follow various aspects.


For thorough details please refer


http://en.wikipedia.org/wiki/Section_508_Amendment_to_the_Rehabilitation_Act_of_1973


This article is specifically targeted for users interested to know


How do we provide support for Screen Readers software with the applications developed in one of the most cutting edge technology Silverlight?


Traditional web based applications that emits html to browsers easily comply with Screen readers based on specifications followed for HTML.
With Silverlight based applications the Silverlight plug-in takes care for screen readers. We just need to follow some methodologies provided in form of Automation Properties in Silverlight. Automation Properties provide several methods and attachable properties which help us define access keys, instruction text for screen readers and much more.


For details please refer


http://msdn.microsoft.com/en-us/library/system.windows.automation.automationproperties(v=vs.95).aspx


By following these properties the work is pretty much simple.


Sample 1

[sourcecode language="html"]
<TextBox id=” nameTextBox” AutomationProperties.Name=”Please enter name” />
[/sourcecode]

Screen reader will voice over “Please enter name” when this textbox receives focus.

Sample 2

[sourcecode language="html"]
<TextBlock id=”nameTextBlock” Text=”Please enter name” />
<TextBox id=”nameTextBox” AutomationProperties.LabeledBy="{Binding ElementName= nameTextBlock }" />
[/sourcecode]

Screen reader will voice over “Please enter name” when this textbox receives focus. Here is the case when we want to directly bind the AutomationProperties.Name property of textbox to any control, in this case a label beside text box.


Pretty much simply till here.

But wait!


Silverlight SDK and Controls are very powerful and includes number of controls that can be bound directly to objects. Here is where most of the screen readers fail while generating voice over for such control. I will depict the problem with one of the control “Combo Box”


Problem
If your combo box is bound to any dictionary object including collection than screen readers will work perfectly. But if combo box is bound to any object like “Person” than screen readers will fail.


Sample Code

[sourcecode language="html"]
<ComboBox Name="personsComboBox" ItemsSource="{Binding}" DisplayMemberPath="Name"/>
[/sourcecode]

 

[sourcecode language="csharp"]
public class Person : INotifyPropertyChanged
{
private string name;
public string Name
{
get
{
return name;
}
set
{
if (value != name)
{
name = value;
OnPropertyChnaged("Name");
}
}
}

private string surname;
public string Surname
{
get
{
return surname;
}
set
{
if (value != surname)
{
surname = value;
OnPropertyChnaged("Surname");
}
}
}

private Int32 age;
public Int32 Age
{
get
{
return age;
}
set
{
if (value != age)
{
age = value;
OnPropertyChnaged("Age");
}
}
}

#region INotifyPropertyChanged Members

public event PropertyChangedEventHandler PropertyChanged;

private void OnPropertyChnaged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}

#endregion

}
[/sourcecode]

Solution

Screen Readers works on strings attached to the controls using attachable property AutomationProperties.Name as described in above example code. With the case were we are binding objects directly to control, screen readers fail and voice over the entire path (Namespace.ObjecName) were the object reside. In order to overcome this issue simply override ToString() method for your objects.


Adding this code with the Person object designed above will ask screen readers to voice over “Name” property.



[sourcecode language="csharp"]
public override string ToString()
{
return Name;
}
[/sourcecode]

Tricky but yet simple.

Enjoy Coding.