[英]Xamarin.Forms ListView change Cell on ContextMenu open
我使用Xamarin.Forms来定义ListView
。 此ListView
在ViewCell
内部定义了一些ContextAction
。 然后,根据平台,将这些上下文动作呈现给用户。 在Android中,这是通过长按特定项目来触发的。 可悲的是,这个项目不会(正确地)突出显示,正如在这个截图中看到的那样(我很久以来第三项 ,遗憾的是我还没有嵌入图像)。
有没有办法在上下文菜单打开时修改Cell? 特别是要求Android的解决方案,但也欢迎一般的答案。 目标最终是改善突出显示,例如通过更改单元格的背景颜色。 当按下一个ContextAction
时,修改单元格不是我想要的。
我浏览了Xamarin.Forms的源代码,并考虑以某种方式继承自例如ViewCell类,但找不到长按项目时会触发/调用的事件或命令。 我已经设置了一个简单的存储库来说明行为: GitHub存储库
最重要的代码片段
XAML中的ListView定义
<?xml version="1.0" encoding="utf-8"?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:ListViewContextMenu" x:Class="ListViewContextMenu.ListViewContextMenuPage"> <ListView x:Name="MyListView"> <ListView.ItemTemplate> <DataTemplate> <ViewCell> <ViewCell.ContextActions> <MenuItem Text="Action" Command="{Binding OnAction}" CommandParameter="{Binding .}"/> </ViewCell.ContextActions> <Label Text="{Binding Name}" /> </ViewCell> </DataTemplate> </ListView.ItemTemplate> </ListView> </ContentPage>
MyItem定义(MVVM)
using System.Diagnostics; using Xamarin.Forms; namespace ListViewContextMenu { public class MyItem { public string Name { get; set; } public Command OnAction { get; set; } public MyItem() { OnAction = new Command((obj) => Debug.WriteLine($"Item {obj.ToString()} clicked")); } } }
无需自定义渲染器 - 您只需将以下标记添加到styles.xml
(位置: Android项目>资源>值> styles.xml )
<style name="MyTheme" parent="MyTheme.Base">
<item name="android:colorLongPressedHighlight">@color/ListViewHighlighted</item>
</style>
<color name="ListViewHighlighted">#A8A8A8</color>
更多细节可以在这篇文章中找到。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.