c# - DataTemplate 中的 Xamarin.Forms CollectionView 项目“背后”是什么(关于背景颜色)?
问题描述
我有一个带圆角的框架作为我的 DataTemplate 的最外层容器......但我选择的项目仍然突出显示框架“背后”的背景......请参阅:
我假设我应该向 VisualStatemanager 添加一个 setter,但我应该针对什么元素来改变它?
这是视图的摘要:
public class MyView : ContentPage
{
public MyView()
{
var mainStackLayout = new StackLayout();
Content = mainStackLayout;
var cv = new CollectionView
{
ItemSizingStrategy = ItemSizingStrategy.MeasureFirstItem,
VerticalOptions = LayoutOptions.FillAndExpand,
SelectionMode = SelectionMode.Multiple,
ItemsLayout = new LinearItemsLayout(ItemsLayoutOrientation.Vertical) { ItemSpacing = 8 }
};
cv.SetBinding(CollectionView.ItemsSourceProperty, "MyList");
cv.SetBinding(CollectionView.SelectedItemsProperty, "MySelectedItems");
cv.SetBinding(CollectionView.SelectionChangedCommandProperty, "MySelectionChangedCommand");
cv.ItemTemplate = new DataTemplate(() =>
{
BackgroundColor = Color.HotPink; //Does nothing...?
var outterFrame = new Frame()
{
IsClippedToBounds = true,
CornerRadius = 25,
Padding = 0,
Margin = new Thickness(3),
BorderColor = Color.Gray
};
var vsg = new VisualStateGroup() { Name = "vsg" };
var vsNormal = new VisualState { Name = "Normal" };
var vsSelected = new VisualState { Name = "Selected" };
vsNormal.Setters.Add(new Setter
{
Property = Frame.BackgroundColorProperty,
Value = Color.White
});
vsSelected.Setters.Add(new Setter
{
Property = Frame.BackgroundColorProperty,
Value = Color.LightBlue
});
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
// I assume I need to add another setter here.... but what's the target?
//
//vsSelected.Setters.Add(new Setter
//{
// Property = _________.BackgroundColorProperty,
// Value = Color.Transparent
//});
//
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
vsg.States.Add(vsNormal);
vsg.States.Add(vsSelected);
VisualStateManager.GetVisualStateGroups(fOutter).Add(vsg);
var grid = new Grid
{
HorizontalOptions = LayoutOptions.FillAndExpand,
Margin = new Thickness(0),
RowSpacing = 0
};
grid.RowDefinitions.Add(new RowDefinition { blah, blah, blah... });
grid.RowDefinitions.Add(new RowDefinition { blah, blah, blah... });
grid.ColumnDefinitions.Add(new ColumnDefinition { blah, blah, blah... });
grid.ColumnDefinitions.Add(new ColumnDefinition { blah, blah, blah... });
grid.ColumnDefinitions.Add(new ColumnDefinition { blah, blah, blah... });
var icon = new Image{ blah, blah, blah... };
var moreStuffForTheItem = blah, blah, blah...
outterFrame.Content = grid;
return outterFrame;
}); //end cv.ItemTemplate
mainStackLayout.Children.Add(cv);
}
}
解决方案
您可以尝试将默认选择高亮颜色设置为透明。
对于 Android,在 android 项目中,在您的主题样式 ( Resources/values/styles
)中添加以下代码
<item name="android:colorActivatedHighlight">@android:color/transparent</item>
对于 ios,您需要创建一个自定义的 viewcell 渲染器。
using System;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer (typeof(ViewCell),typeof(MyAPP.iOS.CustomAllViewCellRendereriOS))]
namespace MyAPP.iOS
{
public class CustomAllViewCellRendereriOS : ViewCellRenderer
{
public override UIKit.UITableViewCell GetCell (Cell item, UIKit.UITableViewCell reusableCell, UIKit.UITableView tv)
{
var cell = base.GetCell (item, reusableCell, tv);
if (cell != null)
cell.SelectionStyle = UIKit.UITableViewCellSelectionStyle.None;
return cell;
}
}
}
推荐阅读
- python-3.x - Python中str类型的find()和index(),哪个时间复杂度更好?
- powershell - PowerShell:带有 PID 的 AppActivate 不起作用
- php - 将变量从 HTML 表单传递到 PHP
- javascript - 在 react/javascript 中修改一个 zip 文件
- python - 模块在 Django 中没有属性
- javascript - h1 元素文本没有使用 javascript 更改,但控制台中的更改
- python - Python 与 Spyder
- html - 我只想在我的 html 页面中插入图片?
- javascript - 有条件的 onclick 函数在没有点击的情况下触发?
- sql - SQL Server - 即使不满足条件,也会显示 LEFT 表中的所有行