如何在xamarin中的Viewcell项目选择中设置自己的颜色.UWP



我正在创建应用程序,因为它包含列表视图,当我尝试运行到iOSAndroid时,它可以与自定义渲染器一起正常工作,但当我尝试在Windows中运行时,它显示蓝色。我必须在项上设置transparent颜色。我尝试制作自定义渲染器,但找不到解决方案。

提前谢谢。

在这种情况下有两个选项。如果您不需要ListView来指示所选项目,只需要知道哪些项目已被点击,请将SelectionMode设置为None:

<ListView SelectionMode="None" ...>

要知道哪个项目已被点击,您现在可以使用ItemTapped事件,它将在ItemTappedEventArgs.Item属性中为您提供数据绑定项目。

如果您想保持选择模式,但想修改所选ListView项目的颜色,也可以这样做。

在文档中检查ListView控件的默认样式和模板。您可以看到,默认情况下,颜色设置为ListViewItemPresenter.SelectedBackground{ThemeResource SystemControlHighlightListAccentLowBrush}。这个笔刷实际上是一个基于当前用户的系统强调颜色的系统范围的笔刷,但您可以覆盖-仅用于特定的ListView或整个应用程序。

如果您想要应用程序范围的覆盖,请在UWP项目头的App.xaml中的应用程序资源级别上声明一个Brush

<Application.Resources>
<SolidColorBrush Color="Red" x:Key="SystemControlHighlightListAccentLowBrush" />
</Application.Resources>

对于控件特定的替代,请创建自定义渲染器和自定义样式。

首先是共享项目中的控制:

public class SelectionColorListView : ListView
{
public static readonly BindableProperty SelectionColorProperty =
BindableProperty.Create(nameof(SelectionColor), typeof(Color), typeof(SelectionColorListView), Color.Green);
public Color SelectionColor
{
get => (Color) GetValue(SelectionColorProperty);
set => SetValue(SelectionColorProperty, value);
}
}

然后是UWP的渲染器:

[assembly: ExportRenderer(typeof(SelectionColorListView), typeof(SelectionColorListViewRenderer))]
namespace App.UWP
{
public class SelectionColorListViewRenderer : ListViewRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<ListView> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
UpdateSelectionColor();
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == nameof(SelectionColorListView.SelectionColor))
{
UpdateSelectionColor();
}
}
private void UpdateSelectionColor()
{
if (Control != null && Element is SelectionColorListView listView)
{
var nativeColor = XamarinColorToNative(listView.SelectionColor);
Control.Resources["SystemControlHighlightListAccentLowBrush"] = new SolidColorBrush(nativeColor);
}
}
private Color XamarinColorToNative(Xamarin.Forms.Color color)
{
var alpha = (byte)(color.A * 255);
var red = (byte)(color.R * 255);
var green = (byte)(color.G * 255);
var blue = (byte)(color.B * 255);
return Color.FromArgb(alpha, red, green, blue);
}
}
}

请注意,不幸的是,颜色在运行时无法更改——一旦第一次完成选择,即使更改了属性值,颜色也将保持不变。

最后,如果您只是希望颜色是"透明的",请使用Color.Transparent作为SelectionColor

对于uwp,您可以简单地将样式添加到uwp项目中的app.xaml文件中。只需在app.xaml-uwp端的ResourceDictionary中添加以下样式即可。

<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Light">
<Color x:Key="SystemAccentColor">#FF0000</Color>
<SolidColorBrush x:Key="SystemControlHighlightAltListAccentHighBrush" Color="{ThemeResource SystemAccentColor}" Opacity="0.9" />
<SolidColorBrush x:Key="SystemControlHighlightAltListAccentLowBrush" Color="{ThemeResource SystemAccentColor}" Opacity="0.6" />
<SolidColorBrush x:Key="SystemControlHighlightAltListAccentMediumBrush" Color="{ThemeResource SystemAccentColor}" Opacity="0.8" />
<SolidColorBrush x:Key="SystemControlHighlightListAccentHighBrush" Color="{ThemeResource SystemAccentColor}" Opacity="0.9" />
<SolidColorBrush x:Key="SystemControlHighlightListAccentLowBrush" Color="{ThemeResource SystemAccentColor}" Opacity="0.6" />
<SolidColorBrush x:Key="SystemControlHighlightListAccentMediumBrush" Color="{ThemeResource SystemAccentColor}" Opacity="0.8" />
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>
</Application.Resources>

最新更新