我无法在 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/