Xamarin.Forms 支持点住手势吗?

Does Xamarin.Forms support tap and hold gesture?

我是 Xamarin.forms 的新手,我的客户想要像 Gmail 这样的功能,用户可以点击并按住其中一个列表项并获得多个 select 项的选项。

该应用程序将有一个项目列表,其中包含不同的可用选项,例如删除、查看、上传等。所以基本上它有 5 个选项,并且根据 windows 移动限制,该应用程序的菜单选项不能超过 4 个(工具栏项)。因此需要点击并按住手势。一旦用户点击并按住其中一项,ToolbarItem 应该更改并替换为仅删除选项。通过这样做,我们可以将 ToolbarItem 减少到四个。

任何参考资料都会有很大帮助!! :-)

还想知道点击并按住是否可行,那么不同平台(iOS、windows、android)将如何渲染它?它会由 Xamarin.forms 处理还是代码中有一些东西必须针对不同的 OS 平台处理?

不幸的是,默认情况下该事件不在其中。然而,有一个便宜的第 3 方组件可以为您处理它:http://www.mrgestures.com/#events 使用 LongPressing 或 LongPressed。

或者您可以选择在每个平台上本地实现它。

您是否考虑过使用 Context Options 而不是替换工具栏中的选项?

如果您可以使用上下文选项而不是工具栏,则不需要第 3 方组件,因为 Xamarin.Forms 允许您轻松地为每个 listView 项目定义此类选项:

实例化您的 ListView

var listView = new ListView();
listView.ItemTemplate = new DataTemplate(typeof(MyListItemCell));

数据模板应如下所示:

public class MyListItemCell : ViewCell
{
    // To register the LongTap/Tap-and-hold gestures once the item model has been assigned
    protected override void OnBindingContextChanged()
    {
        base.OnBindingContextChanged();
        RegisterGestures();
    }

    private void RegisterGestures()
    {
        var deleteOption = new MenuItem()
        {
            Text = "Delete",
            Icon = "deleteIcon.png", //Android uses this, for example
            CommandParameter = ((ListItemModel) BindingContext).Id
        };
        deleteOption.Clicked += deleteOption_Clicked;
        ContextActions.Add(deleteOption);

        //Repeat for the other 4 options

    }
    void deleteOption_Clicked(object sender, EventArgs e)
    {
         //To retrieve the parameters (if is more than one, you should use an object, which could be the same ItemModel 
        int idToDelete = (int)((MenuItem)sender).CommandParameter; 
        //your delete actions
    }
    //Write the eventHandlers for the other 4 options
}