簡體   English   中英

Xamarin表單 - 所選項目的列表視圖將所有背景更改為相同

[英]Xamarin Forms - Listview on selected item change all background to the same

當您選擇一個項目時,我發現一個錯誤,所有子項背景都因相同的顏色而變化。

在所有元素中,我將屬性BackgroundColor。 這只發生在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;
    }

未選擇的項目 未選擇

選定項目 選

有人知道如何解決這個問題嗎?

這是在iOS中選擇單元格時的默認行為。

我們需要ViewCellcustom renderer來禁用該效果。

在iOS項目中創建一個名為NativeiOSCellRenderer的類。

代碼:

[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;      
        }
    }
}  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM