处理单击和双击/点击的按钮手势

时间:2017-03-20 19:03:06

标签: event-handling xamarin.forms double-click

在Xamarin表格中:我希望能够同时检测到点击/点击和双击/点按,并能够执行不同的操作。

也就是说,当点击按钮时,我想执行actionA,当双击该按钮时,我想执行actionB,而只执行actionB

3 个答案:

答案 0 :(得分:0)

你需要一个真正的按钮吗?别看TapGestureRecognizer。在this post中描述了如何使用它。您几乎可以将它应用于任何控件。

例如,如果您将Label设置为按钮的样式,则可以在其上添加双击识别器,如下所示:

<Label Text="Tap me, I double dare you">
    <Label.GestureRecognizers>
        <TapGestureRecognizer
                Tapped="OnTapGestureRecognizerTapped"
                NumberOfTapsRequired="2" />
  </Label.GestureRecognizers>
</Label>

当然,您需要在代码后面有一个名为OnTapGestureRecognizerTapped的事件。但由于NumberOfTapsRequired属性中的值,您需要双击才能激活它。

如果你更喜欢代码,那就是对应的代码:

var tapGestureRecognizer = new TapGestureRecognizer();
tapGestureRecognizer.NumberOfTapsRequired = 2;
tapGestureRecognizer.Tapped += (s, e) => {
    // handle the tap
};
label.GestureRecognizers.Add(tapGestureRecognizer);

答案 1 :(得分:0)

这是我最终做的事情(对Amit Patil的称赞):

def count(this_count):
    return this_count + 1

def total(this_itervar, this_total):
    return this_itervar + this_total

def avg(this_count, this_total):
    if this_count == 0:
        return 0
    else:
        return this_total /this_count

this_count=0
this_total=0
while True:
    try:
        itervar=raw_input('Enter a number: ')
        if  itervar == 'done':
            break
        itervar=float(itervar)
        this_count = count(this_count)
        this_total = total(itervar, this_total)

    except:
        print 'Invalid input'
print str(this_total) + ' ' + str(this_count) + ' ' + str(avg(this_count, this_total))

答案 2 :(得分:0)

ViewModel.cs:

    private bool _isClicked = true;
    public bool IsClicked
    {
        get { return _isClicked = true; }
        set { _isClicked = true = value; OnPropertyChanged(); }
    }

视图.xaml:

<Button Text="Click!" IsEnabled="{Binding IsClicked}" />

方法(在 ViewModel.cs 中):

    public void MyMethod()
    {
        IsClicked = false;


        //Your codes


        IsClicked = true;
    }
相关问题