How to make narrator do not repeat the sentence when off/on content is set at toggle switch in UWP?

Henrique Andrade Mariz 10 Reputation points
2024-02-06T19:34:27.0933333+00:00

I am currently implementing a custom ToggleSwitch in UWP based on Microsoft's template. However, I've encountered a peculiar bug related to the narrator behavior, which seems to be resolved in WinUI 3 but persists in UWP (WinUI 2). I'm seeking assistance to address this issue.

If the OnContent or OffContent is set at the toggle switch it will cause the narrator to repeat itself one time. If you don't use this properties, it reads perfectly, without repeating itself.

<StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
        <ToggleSwitch Header="A" />
        <Grid>
            <ToggleSwitch
                Header="B"
                OffContent="Disable"
                OnContent="Enable"
               />
        </Grid>
    </StackPanel>

In the provided example code, the first ToggleSwitch with the header 'A' behaves correctly. However, when the OnContent and OffContent properties are set, as in the second ToggleSwitch with the header 'B,' the narrator repeats itself one extra time. The expected behavior is to read 'Enable, Toggle Switch, ON' or 'Disable, Toggle Switch, OFF,' but it reads something like 'Enable, Toggle Switch, ON, Enable, Toggle Switch, ON." or the analogue

How can I overcome this issue and prevent the narrator from repeating itself when using the OnContent and OffContent properties in a UWP ToggleSwitch? Any insights or workarounds would be greatly appreciated.

Universal Windows Platform (UWP)
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
781 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Azar 21,225 Reputation points MVP
    2024-02-06T20:04:36.6566667+00:00

    Hai
    Henrique Andrade Mariz

    Thats a great question thanks for posting it in QandA platform

    lemme drop you a workaround to prevent the narrator from repeating itself could be to handle the accessibility announcements manually. like below

    first disable the default accessibility announcements by setting the AutomationProperties.Name property to an empty string.

    and handle the accessibility announcements manually by subscribing to the Toggled event of the ToggleSwitch and using the Narrator API to announce the state change.

    let me update the XAML code and add event handlers .

    <StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">     <ToggleSwitch Header="A" />     
    <Grid>         
    <ToggleSwitch             Header="B"             OffContent="Disable"             OnContent="Enable"             AutomationProperties.Name=""             Toggled="ToggleSwitch_Toggled" />    
     </Grid> 
    </StackPanel> 
    
    
    
    using Windows.UI.Xaml.Automation.Peers; 
    using Windows.UI.Xaml.Controls;  
    private void ToggleSwitch_Toggled(object sender, RoutedEventArgs e) {     ToggleSwitch toggleSwitch = sender as ToggleSwitch;    
     string announcement = toggleSwitch.IsOn ? "Enabled" : "Disabled";     AutomationProperties.SetName(toggleSwitch, announcement);     PeerSettings.AnnounceAccessibilityNotification(PeerSettings.AutomationNotificationKind.Other,         PeerSettings.AutomationNotificationProcessing.ImportantMostRecent, announcement); } 
    
    
    

    If this helps kindly accept the answer thanks much/,


  2. Pinaki Ghatak 2,720 Reputation points Microsoft Employee
    2024-02-07T07:50:08.7266667+00:00

    Hello Henrique Andrade Mariz You could consider creating a custom AutomationPeer for your ToggleSwitch. AutomationPeer classes provide the Microsoft UI Automation system with information about your app’s user interface controls and can help to improve the narrator behavior. Here’s a simple example of how you might implement this:

    public class CustomToggleSwitch : ToggleSwitch
    {
        protected override AutomationPeer OnCreateAutomationPeer()
        {
            return new CustomToggleSwitchAutomationPeer(this);
        }
    }
    
    public class CustomToggleSwitchAutomationPeer : ToggleSwitchAutomationPeer
    {
        public CustomToggleSwitchAutomationPeer(CustomToggleSwitch owner) : base(owner)
        {
        }
    
        protected override string GetClassNameCore()
        {
            return "ToggleSwitch";
        }
    
        protected override string GetNameCore()
        {
            var toggleSwitch = (CustomToggleSwitch)Owner;
            return $"{toggleSwitch.Header}, {toggleSwitch.IsOn ? toggleSwitch.OnContent : toggleSwitch.OffContent}, Toggle Switch, {(toggleSwitch.IsOn ? "On" : "Off")}";
        }
    }
    

    In this example, CustomToggleSwitch is a new class that extends ToggleSwitch and overrides the OnCreateAutomationPeer method to return a new instance of CustomToggleSwitchAutomationPeer. This custom AutomationPeer then overrides GetNameCore to provide a custom name that the narrator will read, which should prevent the repetition you’re experiencing. Please note that this might not cover all scenarios or behaviors of the narrator. It’s always recommended to test thoroughly with different narrator settings and verify the behavior. I hope this answers your question.