ios - Xamarin 窗体 : How to change background from gray on selected item in listview, iOS

标签 ios listview xamarin xamarin.ios xamarin.forms

我无法在 iOS 上将 ListView 中所选项目的背景颜色从灰色更改。我使用 Xamarin.Forms 2.3.4.192-pre2,然后使用 2.3.3 获得相同的结果。

我试图刺穿自定义渲染器:

using App.Views.View.List;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(NoSelectionViewCell), typeof(App.iOS.Views.List.NoSelectionViewCellRenderer))]
namespace App.iOS.Views.List
{
    public class NoSelectionViewCellRenderer: ViewCellRenderer
    {
        public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
        {
            UITableViewCell cell = base.GetCell(item, reusableCell, tv);
            cell.SelectionStyle = UITableViewCellSelectionStyle.None; // This does nothing ...
            // cell.SelectionStyle = UITableViewCellSelectionStyle.Blue; // This does nothing ...
            // cell.SelectionStyle = UITableViewCellSelectionStyle.Default; // This does nothing ...
            // cell.SelectionStyle = UITableViewCellSelectionStyle.Gray; // This does nothing ...
            if (cell != null)
            {
                 cell.SelectedBackgroundView = new UIView
                 {
                     BackgroundColor = UIColor.Red, // This doesn't matter ...
                 };
            }

            UpdateBackground(cell, item); // This doesn't seem to do anything ...

            return cell;
        }
    }
}

我希望背景是透明的。

最佳答案

我在我的项目中所做的是在选择项目后取消选择该项目。它解决了您无法选择已选择的项目的问题(例如,您选择了一个项目,您导航到新页面然后您导航回来并且该项目已被选择。在这种情况下,您无法重新选择相同的项目)。

我就是这样做的:

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

    //we do stuff here

    // We deselect the item so that the background is not greyed when we come back
    ListView.SelectedItem = null;
}

这是xml
<ListView x:Name="ListView" 
              ItemSelected="ListView_OnItemSelected">
</ListView>

关于ios - Xamarin 窗体 : How to change background from gray on selected item in listview, iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42648273/

相关文章:

ios - 是否为AVAssetReaderAudioMixOutput指定基于软件的编解码器?

Android:在选项卡和返回之间切换时,ListView 烦人地滚动到顶部

java - 从 ArrayList<HashMap<String, String>> 获取无效值

c# - Xamarin Forms 可移植应用程序上未显示 OxyPlot 图表

c++ - ld : -bind_at_load and -bitcode_bundle (Xcode setting ENABLE_BITCODE=YES) cannot be used together

ios - 按星期几对字符串数组进行排序

ios - 当类不提供实现时,为什么可以调用 init 的无参数版本?

android - Android ListView 项的滑入按钮效果

ios - 如何针对旧版本的 iOS SDK 构建 Xamarin

ios - 假设静态变量永远不会被清除是否安全?