问题
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