【问题标题】:How to create an "ontouch" event for my button in xamarin mvvmcross?如何在 xamarin mvvmcross 中为我的按钮创建“ontouch”事件?
【发布时间】:2019-02-12 03:38:02
【问题描述】:

在我的项目中,我正在尝试创建一个音频按钮,例如使用 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" />

这是我在视图模型中的代码

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

【问题讨论】:

    标签: xamarin touch mvvmcross ontouch


    【解决方案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
    }
    

    【讨论】:

    • Cheesebaron 我不太明白如何完成代码的实现。我更新了我的问题,所以它更完整,也许你可以告诉我我错了什么
    • 你不明白什么?你一开始的问题描述得非常模糊。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-29
    • 1970-01-01
    相关资源
    最近更新 更多