Споделяне чрез


ListView SelectionMode on Windows

On the Universal Windows Platform, by default the Xamarin.Forms ListView uses the native ItemClick event to respond to interaction, rather than the native Tapped event. This provides accessibility functionality so that the Windows Narrator and the keyboard can interact with the ListView. However, it also renders any tap gestures inside the ListView inoperable.

This Universal Windows Platform platform-specific controls whether items in a ListView can respond to tap gestures, and hence whether the native ListView fires the ItemClick or Tapped event. It's consumed in XAML by setting the ListView.SelectionMode attached property to a value of the ListViewSelectionMode enumeration:

<ContentPage ...
             xmlns:windows="clr-namespace:Xamarin.Forms.PlatformConfiguration.WindowsSpecific;assembly=Xamarin.Forms.Core">
    <StackLayout>
        <ListView ... windows:ListView.SelectionMode="Inaccessible">
            ...
        </ListView>
    </StackLayout>
</ContentPage>

Alternatively, it can be consumed from C# using the fluent API:

using Xamarin.Forms.PlatformConfiguration;
using Xamarin.Forms.PlatformConfiguration.WindowsSpecific;
...

listView.On<Windows>().SetSelectionMode(ListViewSelectionMode.Inaccessible);

The ListView.On<Windows> method specifies that this platform-specific will only run on the Universal Windows Platform. The ListView.SetSelectionMode method, in the Xamarin.Forms.PlatformConfiguration.WindowsSpecific namespace, is used to control whether items in a ListView can respond to tap gestures, with the ListViewSelectionMode enumeration providing two possible values:

  • Accessible – indicates that the ListView will fire the native ItemClick event to handle interaction, and hence provide accessibility functionality. Therefore, the Windows Narrator and the keyboard can interact with the ListView. However, items in the ListView can't respond to tap gestures. This is the default behavior for ListView instances on the Universal Windows Platform.
  • Inaccessible – indicates that the ListView will fire the native Tapped event to handle interaction. Therefore, items in the ListView can respond to tap gestures. However, there's no accessibility functionality and hence the Windows Narrator and the keyboard can't interact with the ListView.

Note

The Accessible and Inaccessible selection modes are mutually exclusive, and you will need to choose between an accessible ListView or a ListView that can respond to tap gestures.

In addition, the GetSelectionMode method can be used to return the current ListViewSelectionMode.

The result is that a specified ListViewSelectionMode is applied to the ListView, which controls whether items in the ListView can respond to tap gestures, and hence whether the native ListView fires the ItemClick or Tapped event.