如何在Xamarin.Forms Android中向我的视图单元格添加披露指示符/复选标记?

Ala*_*an2 5 xamarin xamarin.forms

这是我到目前为止为iOS实现的:

using System;
using Xamarin.Forms;

namespace Japanese
{
    public class ExtCheckedTextCell: TextCell
    {

        public static readonly BindableProperty IsCheckedProperty =
        BindableProperty.Create(
                "IsChecked", typeof(bool), typeof(ExtCheckedTextCell),
            defaultValue: false);

        public bool IsChecked
        {
            get { return (bool)GetValue(IsCheckedProperty); }
            set { SetValue(IsCheckedProperty, value); }
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

我的渲染器看起来像这样:

using System;
using System.ComponentModel;
using System.Diagnostics;
using Japanese;
using Japanese.iOS;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(ExtCheckedTextCell), typeof(ExtCheckedTextCellRenderer))]
namespace Japanese.iOS
{

    public class ExtCheckedTextCellRenderer : TextCellRenderer
    {
        public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
        {
            var nativeCell = base.GetCell(item, reusableCell, tv);

            if (item is ExtCheckedTextCell formsCell)
            {
                SetCheckmark(nativeCell, formsCell);
                SetTap(nativeCell, formsCell);
            }

            return nativeCell;
        }

        protected override void HandlePropertyChanged(object sender, PropertyChangedEventArgs args)
        {
            base.HandlePropertyChanged(sender, args);

            System.Diagnostics.Debug.WriteLine($"HandlePropertyChanged {args.PropertyName}");

            var nativeCell = sender as CellTableViewCell;
            if (nativeCell?.Element is ExtCheckedTextCell formsCell)
            {
                if (args.PropertyName == ExtCheckedTextCell.IsCheckedProperty.PropertyName)
                    SetCheckmark(nativeCell, formsCell);

            }
        }

        void SetCheckmark(UITableViewCell nativeCell, ExtCheckedTextCell formsCell)
        {
            if (formsCell.IsChecked)
                nativeCell.Accessory = UITableViewCellAccessory.Checkmark;
            else
                nativeCell.Accessory = UITableViewCellAccessory.None;
        }

}
Run Code Online (Sandbox Code Playgroud)

这里的参考是使用它的XAML:

<TableSection>
   <local:CheckedTextCell Text="{Binding [6].Name}" IsChecked="{Binding [6].IsSelected}" Tapped="atiSelectValue" />
   <local:CheckedTextCell Text="{Binding [7].Name}" IsChecked="{Binding [7].IsSelected}" Tapped="atiSelectValue" />
   <local:CheckedTextCell Text="{Binding [8].Name}" IsChecked="{Binding [8].IsSelected}" Tapped="atiSelectValue" />
</TableSection>
Run Code Online (Sandbox Code Playgroud)

有没有人有任何想法我如何使用自定义渲染器在Android中实现这一点,或者甚至可以这样做?

这是iOS中的一个示例(不是我的).我希望的是Android可以在右侧显示类似的刻度线.

在此输入图像描述

LeR*_*Roy 3

但你也可以在 xaml 中做到这一点?

这是一个仅限 xaml 的解决方案:) 应该适用于 Android 和 Ios 。

.xaml

<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
             x:Class="StackoverflowQ.Views.MainPage" 
             Title="Driving  & Navigation">
    <ContentPage.Resources>
    </ContentPage.Resources>
    <ScrollView>
        <StackLayout>
            <StackLayout x:Name="Header" BackgroundColor="#efeff4" HorizontalOptions="FillAndExpand" HeightRequest="30" Padding="10">
                <Label Text="NAVIGATION VOICE VOLUME" Margin="0, 0, 0, 5" VerticalOptions="EndAndExpand" />
            </StackLayout>
            <StackLayout Orientation="Horizontal" Padding="10">
                <Label Text="No Voice" TextColor="Black" />
                <Image Source="checkboxchecker.png" IsVisible="{Binding IsCheckBoxVisible}" HorizontalOptions="EndAndExpand" />
                <StackLayout.GestureRecognizers>
                    <TapGestureRecognizer Command="{Binding TapCheckBoxCommand}" NumberOfTapsRequired="1" />
                </StackLayout.GestureRecognizers>
            </StackLayout>
            <BoxView HeightRequest="1" HorizontalOptions="FillAndExpand" BackgroundColor="#efeff4" />
        </StackLayout>
    </ScrollView>
</ContentPage>
Run Code Online (Sandbox Code Playgroud)

视图模型

namespace StackoverflowQ.ViewModels
{
    public class MainPageViewModel : ViewModelBase
    {
        public DelegateCommand TapCheckBoxCommand { get; set; }

        private bool _isCheckBoxVisible;
        public bool IsCheckBoxVisible
        {
            get => _isCheckBoxVisible;
            set => SetProperty(ref _isCheckBoxVisible, value);
        }

        public MainPageViewModel(INavigationService navigationService)
            : base(navigationService)
        {
            Title = "Main Page";

            TapCheckBoxCommand = new DelegateCommand(TapCheckBoxSelected);
        }

        public void TapCheckBoxSelected()
        {
            IsCheckBoxVisible = !IsCheckBoxVisible;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述