Xamarin Forms ListView 复选标记

Xamarin Forms ListView Checkmark

我正在尝试在 Xamarin Forms 中实现 ListView。我们可以检查或选择我们想要的项目的列表。我想一次选择一个项目。

我的 xaml 文件:

ListView x:Name="listview" ItemSelected="OnItemSelected" >
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout HorizontalOptions="StartAndExpand" Orientation="Horizontal">
                        <StackLayout Padding="20,0,0,0"  VerticalOptions="Center"  Orientation="Vertical">
                            <Label Text="{Binding .}" YAlign="Center" FontSize="Medium"   />
                        </StackLayout>
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

我的 xaml.cs 文件:

    public void OnItemSelected (object sender, SelectedItemChangedEventArgs e) {
        if (e.SelectedItem == null) return; 

        // add the checkmark in the event because the item was clicked
       // be able to check the item here

        DisplayAlert("Tapped", e.SelectedItem + " row was tapped", "OK");
        ((ListView)sender).SelectedItem = null;
    }

有更好的方法吗?

我想要这样没有字母和搜索菜单的东西:

您可以尝试使用 XLabs checkbox 或 radioButton 控件,Here 是控件示例列表,您只需通过 NuGet Manager 下载包即可。

如果您只需要检查 1 个项目,请在点击事件中点击项目后执行操作。这是一个例子:

        async void FriendListView_ItemTapped(object sender, ItemTappedEventArgs e)
        {           
            var el = e.Item as ProfileItem;
            SelectedItem = el;
            if (e.Item != null)
            {
               await Navigation.PushAsync(new FriendProfile(el));
            }
            ((ListView)sender).SelectedItem = null; // de-select the row
        }

看看这个https://github.com/ricardoromo/Listview-Multiselect,我制作这个示例是为了模拟 miltiselect listview。

我已经在 xamarin 表单中尝试了多选列表视图,并且它适用于 iOS 和 Android。

这花了我一些时间,所以我写了一篇关于整个原型的博客。你可以在这里找到它:

http://androidwithashray.blogspot.com/2018/03/multiselect-list-view-using-xamarin.html

该博客为您提供了有关使用复选框创建列表视图和选择多个项目的完整信息。希望这对您有所帮助!!