Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
lawrence_shaun_introducing_net_maui_build_and_deploy_crosspl.pdf
Скачиваний:
46
Добавлен:
26.06.2023
Размер:
5.15 Mб
Скачать

Chapter 7 Accessibility

headings and thus providing a far more friendly navigation for those users that rely on screen readers. Headings have a level from 1 to 9 and are represented by the SemanticHeadingLevel enumeration.

SemanticScreenReader

.NET MAUI provides the SemanticScreenReader that enables you to instruct a screen reader to announce some text to the user. This can work especially well if you wish to present instructions to a user or to prompt them if they have paused their interaction.

The SemanticScreenReader provides a static Announce method to perform the announcements, it also provides a Default instance. I

personally like to make use of the scenarios where .NET MAUI provides you with a Current or a Default instance and register this with the app builder to make full use of the dependency injection support. To do this, write the following line of code in your MauiProgram.cs file:

builder.Services.AddSingleton(SemanticScreenReader.Default);

With the screen reader registered, you can announce that the new board was created successfully once the user has tapped on the Save button. You need to open the BoardDetailsPageViewModel.cs file and make the following changes.

Add the read-only field.

private readonly ISemanticScreenReader semanticScreenReader;

Assign a value in your constructor, just applying the bold code to your existing content.

public BoardDetailsPageViewModel(ISemanticScreenReader semanticScreenReader)

{

this.semanticScreenReader = semanticScreenReader;

205

Chapter 7 Accessibility

SaveCommand = new Command( () => Save(),

() => !string.IsNullOrWhiteSpace(BoardName));

}

Call Announce in your Save method, just applying the bold code to your existing content.

private async void Save()

{

var board = new Board

{

Name = BoardName, Layout = new FixedLayout

{

NumberOfColumns = NumberOfColumns, NumberOfRows = NumberOfRows

}

};

semanticScreenReader.Announce($"A new board with the name {BoardName} was created successfully.");

await Shell.Current.GoToAsync( "fixedboard",

new Dictionary<string, object>

{

{ "Board", board}

});

}

206

Chapter 7 Accessibility

If you run your application and save a new board called “My work board,” you will observe that the screen reader will announce “A new board with the name My work board was created successfully.” This gives the user some valuable audible feedback. If you expect the save process to take some time, you can also perform an announcement at the start of the process to keep the user informed.

AutomationProperties

AutomationProperties are the old Xamarin.Forms way of exposing information to the screen readers on each platform. I won’t cover all of the options because some have been replaced by the SemanticProperties section that you just learned about. The following are the important ones that provide a different set of functionality.

AutomationProperties.ExcludedWithChildren

The AutomationProperties.ExcludeWithChildren property allows developers to exclude the element supplied and all its children from the accessibility tree. Setting this property to true will exclude the element and all of its children from the accessibility tree.

AutomationProperties.IsInAccessibleTree

The AutomationProperties.IsInAccessibleTree property allows developers to decide whether the element is visible to screen readers. A common scenario for this feature is to hide controls such as Label or

Image controls that serve a purely decorative purpose (e.g., a background image). Setting this property to true will exclude the element from the accessibility tree.

207