Xamarin.Forms长按效果-如何在隐藏代码中设置命令(NO XAML) [英] Xamarin.Forms Long Press Effect - How to set the Command in Code Behind (NO XAML)

查看:126
本文介绍了Xamarin.Forms长按效果-如何在隐藏代码中设置命令(NO XAML)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

当我使用XAML时,它可以很好地工作,但是我只需要在后面使用代码即可.如何在后面的代码中将此命令添加到Image中?

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?
};

推荐答案

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);

myCommandICommand的地方.

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

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

这篇关于Xamarin.Forms长按效果-如何在隐藏代码中设置命令(NO XAML)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆