在 通用 Windows 平台 上,預設Xamarin.FormsListView會使用原生ItemClick事件來響應互動,而不是原生Tapped事件。 這提供輔助功能,讓 Windows 朗讀程式和鍵盤可以與 ListView互動。 不過,它也會在無法操作的內 ListView 轉譯任何點選手勢。
此 通用 Windows 平台 平臺特定控制中的ListView專案是否可以響應點選手勢,以及原生ListViewItemClick觸發 或 Tapped 事件。 它會在 XAML 中取用,方法是將 ListView.SelectionMode 附加屬性設定為 列舉的值 ListViewSelectionMode :
<ContentPage ...
xmlns:windows="clr-namespace:Xamarin.Forms.PlatformConfiguration.WindowsSpecific;assembly=Xamarin.Forms.Core">
<StackLayout>
<ListView ... windows:ListView.SelectionMode="Inaccessible">
...
</ListView>
</StackLayout>
</ContentPage>
或者,您可以使用 Fluent API 從 C# 取用它:
using Xamarin.Forms.PlatformConfiguration;
using Xamarin.Forms.PlatformConfiguration.WindowsSpecific;
...
listView.On<Windows>().SetSelectionMode(ListViewSelectionMode.Inaccessible);
方法ListView.On<Windows>會指定這個平臺專用只會在 通用 Windows 平台 上執行。 命名空間 ListView.SetSelectionMode 中的 Xamarin.Forms.PlatformConfiguration.WindowsSpecific 方法可用來控制 中的 ListView 專案是否可以響應點選手勢,而 ListViewSelectionMode 列舉會提供兩個可能的值:
Accessible– 表示ListView會引發原生ItemClick事件來處理互動,因此提供輔助功能功能。 因此,Windows 朗讀程式和鍵盤可以與ListView互動。 不過,中的ListView專案無法回應點選手勢。 這是 通用 Windows 平台 實例的預設行為ListView。Inaccessible– 表示ListView會引發原生Tapped事件來處理互動。 因此,中的ListView專案可以響應點選手勢。 不過,沒有輔助功能功能,因此 Windows 朗讀程式和鍵盤無法與ListView互動。
注意
和 Accessible Inaccessible 選取模式互斥,您必須在可存取ListViewListView或可以響應點選手勢的 之間選擇。
此外, GetSelectionMode 方法可以用來傳回目前的 ListViewSelectionMode。
結果是,指定的 ListViewSelectionMode 會套用至 ListView,這會控制 中的 ListView 專案是否可以響應點選手勢,因此原生 ListView 是否引發 ItemClick 或 Tapped 事件。