我可以在不丢失触觉(触摸)反馈的情况下更改ListView单元格中的背景色吗?
(我在网上询问,但没有得到回应,所以我在这里尝试) 在Xamarin表单中,在ListView我可以在不丢失触觉(触摸)反馈的情况下更改ListView单元格中的背景色吗?,listview,xamarin,xamarin.forms,Listview,Xamarin,Xamarin.forms,(我在网上询问,但没有得到回应,所以我在这里尝试) 在Xamarin表单中,在ListViewItemTemplate中设置BackgroundColor 有什么办法吗?我想自定义列表项的颜色,但没有触觉反馈看起来像垃圾 示例XAML: <ListView x:Name="list" ItemTapped="OnItemSelected" IsGroupingEnabled="True" GroupDisplayBindi
ItemTemplate
中设置BackgroundColor
有什么办法吗?我想自定义列表项的颜色,但没有触觉反馈看起来像垃圾
示例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>
我得到的最接近的结果是在查看单元格中更改背景颜色。点击,然后在视图中重新更改背景颜色。OnAppearing()
(查看单元格。出现
),但这会在抬起手指而不是按下手指时更改背景
我正在Android上测试,但更喜欢跨平台解决方案。一旦设置了背景色,除非在每个平台上创建自定义渲染,否则您将无法看到所选项目的颜色或触觉反馈。
以下是使用绑定设置所选项目颜色时可以实现的跨平台、基于MVVM的解决方案:
将属性绑定到ListView
的SelectedItem
李>
将StackLayout
的背景色绑定到自动属性,并设置“#cccc”的值(如果未选中该项,则为其他颜色)李>
您需要订阅MyItem
类中的SelectedMyItem属性,并调用BackgroundColor属性的OnPropertyChanged
。为此,您可以引发一个事件并订阅或任何您觉得合适的内容(下面提供的示例伪代码中没有实现这一点)
视图模型:
public class MyViewModel : IMyViewModel
{
public ObserveableCollection<MyItem> Items { get; set; }
public MyItem SelectedMyItem { get; set; }
}
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>
您甚至可以通过尝试合并(可能是更改颜色的方法)来进一步改进这一点 查看你在Xamarin论坛上的帖子,获取我的反馈。我有一个简单的解决方案,叫做XFGloss。它是Xamarin.Forms的免费开源插件。该来源可在上获得。还有一个可用的。它允许您为标准XF单元格控件(TextCell、EntryCell等)指定BackgroundColor值。XFGloss还添加了其他一些新特性
如果您希望修复现有的实现,您可以查看我所做的更改,以支持本文中的可视反馈。您只需在Viewcell继承类中使用它即可
this.Tapped+=ViewCell\u Tapped代码>//通过在viewcell中使用tapped,可以实现类似触觉的效果
私有异步void ViewCell_(对象发送器,
事件参数(e)
{
this.View.BackgroundColor=Color.Gray;
等待任务。延迟(10);
this.View.BackgroundColor=Color.White;
}
这对我有效。这会更改所选项目的背景,这与触觉反馈不同。即使你在短时间后重新设置背景颜色(如问题中的简单建议),但在抬起手指而不是按下手指后,背景仍然会发生变化,这感觉不对劲。我还为触觉反馈添加了一个变通方法。如果需要完美的实现,最好创建自定义渲染。您可以从github检查现有的实现并对其进行扩展。该解决方案正是我在问题和我的上述评论中提到的……那么,您的问题的答案是否定的,除非您已经准备好通过自定义渲染器进行此操作。这并不能提供问题的答案。一旦你有足够的钱,你将能够;相反抱歉,罗伯特。我已经编辑了我的回复,通过链接到代码提交来包含答案,在代码提交中,我修复了我的实现中他询问的同一个问题。我仍然没有机会尝试,但我现在将此标记为已接受。恭喜你获得了赏金,欢迎来到stackoverflow!只是一个更新:它的工作原理完全符合广告!非常感谢你,汤米!太棒了,这是个好消息!谢谢你让我知道。在问题中,它已经说“我得到的最接近的结果是在ViewCell中更改背景颜色。点击,然后在View中更改背景颜色。OnAppearing()…,但这会在抬起手指而不是按下手指时更改背景。”
<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>
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
}