如何在Xamarin mvvmcross中为我的按钮创建“ ontouch”事件?

时间:2019-02-11 15:29:21

标签: xamarin touch mvvmcross ontouch

在我的项目中,我试图创建一个音频按钮,就像一个使用whatsap的音频按钮,当您按住开始录制和放下停止录制时,我发现了解决方案,他使用了2个按钮,一个开始并一个完成。我需要的是在按下和释放时使用相同的按钮执行代码。我没有找到要捕获的事件的任何实现。你能帮我吗? 这是我在axml文件中的按钮

 <android.support.design.widget.FloatingActionButton
                android:id="@+id/btn_record"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|right"
                android:src="@drawable/ic_micro"
                android:layout_marginRight="15dp"
                android:layout_marginBottom="15dp"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true"
                android:theme="@style/ControlsTheme"
                local:MvxBind="Click RecordAudioClick; Visibility Visibility(RecordAudioVisibility); Touch Touch" />

这是我在viewmodel中的代码

  public MvxCommand Touch
    {
        get
        {
            return new MvxCommand(() =>
            {
                UserDialogs.Instance.Toast(new ToastConfig(Pressed Button")
                 .SetDuration(3000)
                 .SetMessageTextColor(System.Drawing.Color.White)
                 .SetBackgroundColor(System.Drawing.Color.Black)
                 .SetPosition(ToastPosition.Top));
            });
        }
    }

1 个答案:

答案 0 :(得分:1)

在Android上,您可以订阅Touch事件:

button.Touch += OnButtonTouch;

private void OnButtonTouch(object sender, View.TouchEventArgs args)
{
    var handled = false;
    if (args.Event.Action == MotionEventActions.Down)
    {
        // do stuff when pressed
        handled = true;
    }
    else if (args.Event.Action == MotionEventActions.Cancel ||
             args.Event.Action == MotionEventActions.Up)
    {
        // do stuff when released
        handled = true;
    }

    args.Handled = handled;
}

在iOS上,这段代码有点类似:

button.TouchDown += OnButtonTouchDown;
button.TouchUpInside += OnButtonTouchUpInside;

private void OnButtonTouchDown(object sender, EventArgs e)
{
    // do stuff when pressed
}

private void OnButtonTouchUpInside(object sender, EventArgs e)
{
    // do stuff when released
}