Xamarin.Forms 长按效果 - 如何在代码隐藏中设置命令(NO XAML)

Xamarin.Forms Long Press Effect - How to set the Command in Code Behind (NO XAML)

我正在使用此解决方案来处理长按事件:https://alexdunn.org/2017/12/27/xamarin-tip-xamarin-forms-long-press-effect/

当我使用 XAML 时它工作正常,但我只需要使用代码隐藏。如何将此命令添加到后面代码中的 Image

这是创建图像的代码:

var image = new Image
{
    ClassId = item.Path,
    Aspect = Aspect.AspectFill,
    Source = item.ThumbNailImage,
    Rotation = 90,
    Margin = 10,
    GestureRecognizers = { _tgr },
    //Command here, but how?
};
Microsoft 网站上的

This documentation 非常有助于解释如何在代码中设置附加属性。

因此根据示例,您的代码应如下所示:

image.Effects.Add(new LongPressedEffect());
LongPressedEffect.SetCommand(image, myCommand);

其中 myCommand 是一个 ICommand

这应该创建 LongPressedEffect,将其添加到图像,然后设置确定行为的附加 ICommand