ListView Tapped删除数据模板Xamarin.Forms上的StackLayout背景颜色

问题描述 投票:0回答:1

我不知道为什么,但是当我点击任何ListView项时,其模板中的StackLayout会丢失背景颜色。

我在ListView中使用默认的ViewCell。这是一个Xamarin.Forms错误吗?

我只在Android上遇到此问题。

    <StackLayout>             
            <ListView x:Name="Llist"
            ItemTapped="Lista_ItemTapped" >
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout BackgroundColor="{Binding color}">
                                <Label Text="{Binding name}"/>
                                <Label Text="{Binding age}"/>
                                <Label Text="{Binding sex}"/>

                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>      
            <!--<Button x:Name="excutar" Text="Executar"/>-->
        </StackLayout>
    </ContentPage>
xaml xamarin xamarin.forms xamarin.android
1个回答
0
投票

您必须添加自定义视图单元格,您可以在其中设置单击颜色:

在你的PCL中:

添加自定义ViewCell,如下所示:

 public class ExtendedViewCell : ViewCell
{
    /// <summary>
    /// The SelectedBackgroundColor property.
    /// </summary>
    public static readonly BindableProperty SelectedBackgroundColorProperty =
        BindableProperty.Create("SelectedBackgroundColor", typeof(Color), typeof(ExtendedViewCell), Color.Default);

    /// <summary>
    /// Gets or sets the SelectedBackgroundColor.
    /// </summary>
    public Color SelectedBackgroundColor
    {
        get { return (Color)GetValue(SelectedBackgroundColorProperty); }
        set { SetValue(SelectedBackgroundColorProperty, value); }
    }
}

在您的Android项目中添加以下内容:

public class ExtendedViewCellRenderer : ViewCellRenderer
{

    private Android.Views.View _cellCore;
    private Drawable _unselectedBackground;
    private bool _selected;

    protected override Android.Views.View GetCellCore(Cell item, Android.Views.View convertView, ViewGroup parent, Context context)
    {
        try
        {
            _cellCore = base.GetCellCore(item, convertView, parent, context);

            // Save original background to roll-back to it when not selected,
            // we're assuming that no cells will be selected on creation.
            _selected = false;
            _unselectedBackground = _cellCore.Background;

            return _cellCore;
        }
        catch(Exception ex)
        {
            AppLogger.LogException(ex);
            return null;
        }
    }

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

            if (args.PropertyName == "IsSelected")
            {
                // Had to create a property to track the selection because cellCore.Selected is always false.
                _selected = !_selected;

                if (_selected)
                {
                    var extendedViewCell = sender as ExtendedViewCell;
                    _cellCore.SetBackgroundColor(extendedViewCell.SelectedBackgroundColor.ToAndroid());
                }
                else
                {
                    _cellCore.SetBackground(_unselectedBackground);
                }
            }
        }
        catch(Exception ex)
        {
            AppLogger.LogException(ex);
        }
    }
}

类似于iOS,如下所示:

public class ExtendedViewCellRenderer : ViewCellRenderer
{
    public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
    {
        try
        {
            var cell = base.GetCell(item, reusableCell, tv);
            var view = item as ExtendedViewCell;
            cell.SelectedBackgroundView = new UIView
            {
                BackgroundColor = view.SelectedBackgroundColor.ToUIColor(),
            };
            cell.SelectionStyle = UITableViewCellSelectionStyle.None;
            return cell;
        }
        catch(Exception ex)
        {
            AppLogger.LogException(ex);
            return null;
        }
    }

}

现在不要忘记在命名空间上方的本机类上添加自定义渲染器标头

[assembly: ExportRenderer(typeof(ExtendedViewCell), typeof(ExtendedViewCellRenderer))]

现在您要做的就是用这个控件视图单元替换上面的ViewCell并传递SelectedBackgroundProperty。

在您的情况下,它将如下所示:

<ListView x:Name="Llist"
        ItemTapped="Lista_ItemTapped" >
            <ListView.ItemTemplate>
                <DataTemplate>
                    <nameSpace:ExtendedViewCell SelectedBackgroundColor="White">
                        <StackLayout BackgroundColor="{Binding color}">
                            <Label Text="{Binding name}"/>
                            <Label Text="{Binding age}"/>
                            <Label Text="{Binding sex}"/>

                        </StackLayout>
                    </nameSpace:ExtendedViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>      
© www.soinside.com 2019 - 2024. All rights reserved.