(我问过 Xamarin论坛 但没有得到回应,所以我在这里尝试)
在Xamarin Forms中,设置 BackgroundColor
在ListView中 ItemTemplate
导致触觉反馈被禁用。
有没有办法解决? 我想自定义列表项的颜色,但没有触觉反馈看起来像垃圾。
示例XAML:
<ListView x:Name="list"
ItemTapped="OnItemSelected"
IsGroupingEnabled="True"
GroupDisplayBinding="{Binding Key}"
GroupShortNameBinding="{Binding Key}"
HasUnevenRows="True"
ItemsSource="{Binding .}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout VerticalOptions="FillAndExpand"
Padding="5, 20"
BackgroundColor="#CCCCCC"> <!--This line causes haptic feedback to fail -->
<Label Text="{Binding Name}"
TextColor="Black"
VerticalOptions="Center"
FontSize="Large"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
我最接近的是改变了 BackgroundColor
在 ViewCell.Tapped
,然后改回来 View.OnAppearing()
(ViewCell.Appearing
被打破),但这会在手指抬起时改变背景,而不是在按下时改变背景。
我正在Android上测试,但更喜欢跨平台解决方案。
在Xamarin论坛上查看您的帖子以获取我的反馈。我有一个简单的插入式解决方案,叫做XFGloss。它是Xamarin.Forms的免费开源插件。来源可用 GitHub上。还有一个 NuGet包 可用。它允许您将BackgroundColor值分配给标准XF单元格控件(TextCell,EntryCell等)。 XFGloss还添加了其他几个新属性。
如果您希望修复现有实施,可以查看我为支持视觉反馈所做的更改 代码提交。
在Xamarin论坛上查看您的帖子以获取我的反馈。我有一个简单的插入式解决方案,叫做XFGloss。它是Xamarin.Forms的免费开源插件。来源可用 GitHub上。还有一个 NuGet包 可用。它允许您将BackgroundColor值分配给标准XF单元格控件(TextCell,EntryCell等)。 XFGloss还添加了其他几个新属性。
如果您希望修复现有实施,可以查看我为支持视觉反馈所做的更改 代码提交。
设置背景颜色后,除非在每个平台上创建自定义渲染,否则您将无法看到所选项目颜色或触觉反馈。
以下是您可以实现的跨平台和基于MVVM的工作,使用Bindings来设置所选项目的颜色:
- 将财产绑定到
SelectedItem
的 ListView
。
- 绑定你的背景颜色
StackLayout
到一个自动属性并设置“#CCCCCC”的值,如果它没有选择项目其他颜色。
- 您需要订阅中的SelectedMyItem属性
MyItem
类和调用 OnPropertyChanged
BackgroundColor属性。为此,您可以举办活动和订阅或任何您觉得舒服的事情。(这不在下面提供的示例伪代码中实现)
ViewModel:
public class MyViewModel : IMyViewModel
{
public ObserveableCollection<MyItem> Items { get; set; }
public MyItem SelectedMyItem { get; set; }
}
列表项目类:
public class MyItem
{
private readonly IMyViewModel _myViewModel;
public MyItem(IMyViewModel myViewModel)
{
_myViewModel = myViewModel;
}
public string Name { get; set; }
public Color BackgroundColor => _myViewModel.SelectedMyItem == this
? Color.Red : Color.FromHex("CCCCCC");
}
XAML:
<ListView x:Name="list"
IsGroupingEnabled="True"
GroupDisplayBinding="{Binding Key}"
GroupShortNameBinding="{Binding Key}"
HasUnevenRows="True"
ItemsSource="{Binding Items}"
SelectedItem="{Binding SelectedMyItem}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout VerticalOptions="FillAndExpand"
Padding="5, 20"
BackgroundColor="{Binding BackgroundColor}">
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Tapped="OnItemTapped" />
</StackLayout.GestureRecognizers>
<Label Text="{Binding Name}"
TextColor="Black"
VerticalOptions="Center"
FontSize="Large"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
触觉反馈解决方案:
通常触摸是200-300ms。轻触项目后,您可以在很长时间内更改背景颜色,然后设置为原始颜色。
public async void OnItemTapped(object sender, EventArgs args)
{
BackgroundColor = Color.Orange; //touch color
await Task.Delay(250); //time to show new color
BackgroundColor = Color.Red; //original color
}
您甚至可以通过尝试合并来进一步改进 动画也许是 FadeTo 改变颜色。
您可以在Viewcell继承的类中使用它,
this.Tapped += ViewCell_Tapped;
//通过在viewcell中使用tapped,您可以实现触觉。
private async void ViewCell_Tapped(object sender,
EventArgs e)
{
this.View.BackgroundColor = Color.Gray;
等待Task.Delay(10);
this.View.BackgroundColor = Color.White;
}
这对我有用。