How to add Access key for ToolbarItem on Xamarin UWP ?

Guillaume Dewaghe 21 Reputation points
2022-11-03T16:44:56.727+00:00

Hello everyone!

I'm currently trying to add shortcuts for the elements of my application with Xamarin on UWP.

I saw that we can add shortcuts with Access Key by adding the namespace "Xamarin.Forms.PlatformConfiguration.WindowsSpecific" and "windows:VisualElement.AccessKey" to the element.

Unfortunately, that doesn't work on ToolbarItem so I tried another strategy by modified the style of AppBarButton, which seems the renderer on UWP, by customizing the FormsCommandBarStyle from: https://github.com/xamarin/Xamarin.Forms/blob/5.0.0/Xamarin.Forms.Platform.UAP/FormsCommandBarStyle.xaml#L758

I added those lines to add the Access Key on my custom FormsCommandBarStyle:

<ItemsControl.ItemContainerStyle>  
     <Style TargetType="AppBarButton">  
          <Setter Property="AccessKey" Value="{Binding RelativeSource={RelativeSource Mode=Self}, Path=(AutomationProperties.AutomationId)}"/>  
     </Style>  
</ItemsControl.ItemContainerStyle>  

Unfortunately, that doesn't work too, as if it couldn't find the AutomationId value which is defined on my ToolbarItem element :

<ToolbarItem x:Name="QRCodeButton" Icon="{AppThemeBinding Light=qrscanner.png, Dark=qrscanner_dark.png}" Text="{x:Static res:AppResources.ButtonQRCodes}" Order="Primary" Priority="1" Clicked="QRCodes_Clicked" AutomationId="BA"/>  

The only case this works it's when I add a compact value on the style of AppBarButton.
Do you know if there are other ways to achieve this?

Thanks in advance!

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,294 questions
Universal Windows Platform (UWP)
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 26,316 Reputation points Microsoft Vendor
    2022-11-04T06:58:11.417+00:00

    Hello @Guillaume Dewaghe ,

    As described at the document- VisualElement Access Keys on Windows - Xamarin | Microsoft Learn: This UWP platform-specific is used to specify an access key for a VisualElement. But the inheritance of ToolbarItem (Object->BindableObject->Element->BaseMenuItem->MenuItem->ToolbarItem) doesn't include VisualElement, you cannot set the Access key for ToolbarItem by this way.

    ToolbarItem class is a special type of button, you could try to custom the NavigationPage.TitleView and set the attached property to a layout class that contains multiple buttons, then set the Access key for the Button. For more details, you can refer to Displaying Views in the Navigation Bar and Xamarin.Forms ToolbarItem - Xamarin | Microsoft Learn.

    Best Regards,
    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful