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

徘徊边缘 提交于 2019-12-23 02:57:12

问题


I'm using this solution for handling the long tap event: https://alexdunn.org/2017/12/27/xamarin-tip-xamarin-forms-long-press-effect/

It works fine when I use XAML, but I need to use code behind only. How can I add this command to the Image in the code behind?

Here is the code that creates my image:

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

回答1:


This documentation on Microsoft's website is very helpful in explaining how to set attached properties in code.

So according to the example, your code should look something like this:

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

Where myCommand is an ICommand.

This should create the LongPressedEffect, add it to the image, and then set the attached ICommand that determines the behavior.



来源:https://stackoverflow.com/questions/50695103/xamarin-forms-long-press-effect-how-to-set-the-command-in-code-behind-no-xaml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!