【问题标题】:Xamarin.Forms iOS listview selected item colorXamarin.Forms iOS listview 选定项颜色
【发布时间】:2017-12-17 02:09:39
【问题描述】:

我需要在我的 Xamarin.Forms 应用程序中更改列表视图的选定项目颜色。 所以我创建了一个自定义渲染器...

PCL C#:

public class DarkViewCell : ViewCell {}

PCL XAML:

<ListView>
  <ListView.ItemTemplate>
    <DataTemplate>
      <local:DarkViewCell>
        <ViewCell.View>
          ... Stuff ...
        </ViewCell.View>
      </local:DarkViewCell>            
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>

iOS

public class DarkViewCellRenderer : ViewCellRenderer
{
    private UIView bgView;

    public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
    {
        var cell = base.GetCell(item, reusableCell, tv);

        cell.BackgroundColor = UIColor.Black;
        cell.TextLabel.TextColor = UIColor.White;

        if (bgView == null)
        {
            bgView = new UIView(cell.SelectedBackgroundView.Bounds);
            bgView.Layer.BackgroundColor = UIColor.FromRGB(48,48,48).CGColor;
            bgView.Layer.BorderColor = UIColor.FromRGB(48, 48, 48).CGColor;
            bgView.Layer.BorderWidth = 2.0f;
        }

        cell.SelectedBackgroundView = bgView;

        return cell;
    }
}

但是不工作。我也试图改变 SelectionStyle 但什么都没有......

编辑

在一个新项目中它可以工作。而且代码是一样的

【问题讨论】:

    标签: c# ios listview xamarin.ios xamarin.forms


    【解决方案1】:

    我不是 100% 确定,但是我删除了

    <x:Arguments>
        <ListViewCachingStrategy>RecycleElement</ListViewCachingStrategy>
    </x:Arguments>
    

    它开始工作了。

    【讨论】:

      【解决方案2】:

      我最近遇到了类似的问题,我发现设置单元格背景颜色的最佳方法是使用单元格ContentView

      尝试在您的 GetCell 方法中使用以下内容

      cell.ContentView.BackgroundColor = UIColor.Black;
      

      【讨论】:

        【解决方案3】:

        按照下面的代码,它对我有用

         public override UITableViewCell GetCell(Cell item, UITableView tv)
          {
            var cell = base.GetCell(item, tv);
        
             cell.SelectedBackgroundView = new UIView {
             BackgroundColor = UIColor.DarkGray,
           };
          return cell;
         }
        

        以下链接对你有用

        Xamarin.Forms ListView: Set the highlight color of a tapped item

        【讨论】:

          【解决方案4】:

          尝试设置:

          public override UITableViewCell GetCell(Cell item, UITableView tv)
          {
              var cell = base.GetCell(item, tv);
              cell.SelectedBackgroundView = new UIView() { BackgroundColor = UIColor.Black };
              return cell;
          }
          

          更新: 我只是在一个虚拟项目中使用了您的代码,它可以正常工作。是否添加了组装属性来注册自定义渲染器?

          [assembly: ExportRenderer(typeof(DarkViewCell), typeof(DarkViewCellRenderer))]
          namespace MyProject.iOS
          {
              public class DarkViewCellRenderer : ViewCellRenderer
              {
              }
          }
          

          【讨论】:

          • 自定义渲染器中的其他内容是否正常工作或根本不工作?我刚刚从我自己的一个项目中复制了那段代码,它完全按照需要工作。
          • 更新了我的答案。
          • Assembly 属性存在,断点按预期工作。我正在使用模拟器,这可能是一个错误吗?你在用什么?
          • 也是模拟器。您可以尝试删除 标签,它们是不必要的,但我怀疑这会解决它。
          • 文档中也有这一行:“对于大多数 Xamarin.Forms 元素,可以选择在每个平台项目中提供自定义渲染器。如果未注册自定义渲染器,则将使用控件基类的默认渲染器。但是,在渲染 ViewCell 元素时,每个平台项目都需要自定义渲染器。 - 如果您有 Android 平台项目,是否也有 Android 渲染器?
          猜你喜欢
          • 2018-05-28
          • 2016-11-26
          • 2019-02-11
          • 2013-01-28
          • 1970-01-01
          • 1970-01-01
          • 2014-11-11
          • 1970-01-01
          • 2014-03-07
          相关资源
          最近更新 更多