Xamarin表单-所选项目的Listview将所有背景更改为相同 [英] Xamarin Forms - Listview on selected item change all background to the same

查看:117
本文介绍了Xamarin表单-所选项目的Listview将所有背景更改为相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您选择一个项目时,所有子背景都变为相同颜色时,我发现了一个错误.

I found a error when you select a item all the children backgrounds are change for the same color.

在所有元素中,我都放置了BackgroundColor属性.这只发生在iOS

In all elements, I put the property BackgroundColor. This only happened in the iOS

遵循代码示例:

XAML

<ListView
        x:Name="ListPainel"
        SeparatorColor="#d2d8e2"
        SeparatorVisibility="Default"
        Margin="0"
        ItemsSource="{Binding ListPainel_Source}"
        HasUnevenRows="true"
        RefreshCommand="{Binding ListPainel_RefreshCommand}"
        IsRefreshing="{Binding ListPainel_IsRefreshing}"

    >
    </ListView>

ViewCell的一部分

        protected override void OnBindingContextChanged()
    {
        base.OnBindingContextChanged();

        dynamic temp = BindingContext;

        PainelDto painel = (PainelDto)temp;

        ...

        if(painel.HasDetalhes)
        {
            Button detalhes = new Button()
            {
                Text="VER DETALHES",
                FontSize = 12,
                TextColor = Color.FromHex("#4482ff"),
                HorizontalOptions = LayoutOptions.End,
                VerticalOptions = LayoutOptions.Start,
                HeightRequest = 20,
                WidthRequest = 120,
                BackgroundColor = Color.DeepPink
            };
            detalhes.SetBinding(
                Button.CommandProperty
                , new Binding(
                    "ViewDetalhesCommand"
                    , BindingMode.Default
                    , null
                    , null
                    , null
                    , _viewModelPainel
                )
            );
            box.Children.Add(detalhes);
        }

        ...

        View = box;
    }

未选中的项目

所选项目

有人知道如何解决这个问题吗?

Some one know how to fix this?

推荐答案

这是在iOS中选择单元格时的默认行为.

It is the default behavior when selecting cell in iOS.

ViewCell我们需要custom renderer才能禁用效果.

We need a custom renderer for ViewCell to disable the effect.

在iOS项目中创建一个名为NativeiOSCellRenderer的类.

Create a Class called NativeiOSCellRenderer in iOS project.

代码:

[assembly: ExportRenderer(typeof(ViewCell), typeof(NativeiOSCellRenderer))]
namespace FormsListViewSample.iOS
{
    class NativeiOSCellRenderer : ViewCellRenderer
    {
        public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
        {
            UITableViewCell cell = base.GetCell(item, reusableCell, tv);
            cell.SelectionStyle = UITableViewCellSelectionStyle.None;
            return cell;      
        }
    }
}  

这篇关于Xamarin表单-所选项目的Listview将所有背景更改为相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆