工作中遇到这样的一个需求,模仿微信发生语音功能,需要实现按钮按下开始录音,按钮释放录音结束,
Xamarin.Forms中Button没有这样的事件,那么我们如何实现对Button按下,释放两个事件的监听呢?
当然这里我们再次用到了CustomRenderer,一旦遇到Xamrin.Forms无法实现的某些功能,我们就可以
通过CustomRenderer来处理。
首先:我们自定义一个VoiceRecordButton继承于Button
1 using System; 2 using System.Collections.Generic; 3 using System.Diagnostics; 4 using System.Text; 5 using System.Windows.Input; 6 using Xamarin.Forms; 7 8 namespace XFPractice.CustomView 9 { 10 public class VoiceRecordButton: Button 11 { 12 13 public ICommand StartCommand { get; set; } 14 public ICommand EndCommand { get; set; } 15 16 public VoiceRecordButton() 17 { 18 StartCommand = new Command(()=> { 19 Debug.WriteLine(" StartCommand =========== "); 20 }); 21 22 EndCommand = new Command(() => { 23 Debug.WriteLine(" EndCommand =========== "); 24 }); 25 } 26 27 } 28 }