首页 技术 正文
技术 2022年11月20日
0 收藏 591 点赞 4,403 浏览 2551 个字

提问:监控按钮的点击事件,可以通过按钮的Click事件,或者Command绑定,那么如何监控按钮的按下与抬起,或者移动,长按,双击等事件?

解决方法:各个平台自定义渲染依赖注入.

共享项目PCL:

1先定义一个继承Button的实体类NewButton.cs

    public class NewButton : Button
{
public event EventHandler Pressed;
public event EventHandler Released; public virtual void OnPressed()
{
Pressed?.Invoke(this, EventArgs.Empty);
} public virtual void OnReleased()
{
Released?.Invoke(this, EventArgs.Empty);
}
}

2定义一个NewButton的控件TestControl(相当新建一个页面,只不过是个部分页面,继承contentView而不是contentpage )

xaml:

<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Sample.TestControl">
<ContentView.Content>
<StackLayout x:Name="TheContent">
</StackLayout>
</ContentView.Content>
</ContentView>

cs:

    public partial class TestControl: ContentView
{
public TestControl()
{
InitializeComponent();
var myButton = new NewButton
{
Text = "自定义按钮",
HorizontalOptions = LayoutOptions.FillAndExpand
};
TheContent.Children.Add(myButton); myButton.Pressed += (sender, args) =>
{
myButton.BackgroundColor = Color.Red;
};
myButton.Released += (sender, args) =>
{
myButton.BackgroundColor = Color.Blue;
};
}
}

Android项目:

添加一个NewButtonRenderer.cs文件自定义渲染并注入:

[assembly: ExportRenderer(typeof(NewButton), typeof(NewButtonRenderer))]
namespace Sample.Droid
{
class NewButtonRenderer: ButtonRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e); var TestButton = e.NewElement as NewButton; var thisButton = Control as Android.Widget.Button;
thisButton.Touch += (object sender, TouchEventArgs args) =>
{
if (args.Event.Action == MotionEventActions.Down)
{
TestButton.OnPressed();
}
else if (args.Event.Action == MotionEventActions.Up)
{
TestButton.OnReleased();
}
};
}
}
}

 Ios项目:

类似安卓项目,也是自定义渲染和注入

[assembly: ExportRenderer(typeof(NewButton), typeof(NewButtonRenderer))]
namespace Sample.iOS
{
public class NewButtonRenderer : ButtonRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
{
base.OnElementChanged(e); var TestButton = e.NewElement as NewButton; var thisButton = Control as UIButton;
thisButton.TouchDown += delegate
{
TestButton.OnPressed();
};
thisButton.TouchUpInside += delegate
{
TestButton.OnReleased();
};
}
}
}

这样共享项目和2个平台之间的方法就实现了统一,在按下与抬起的时候,都能够监控事件

用法: 

在布局页面引入控件(上述TestControl)所在的命名空间,然后引入控件即可,例:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:controls="clr-namespace:Sample"
x:Class="Sample.TestPage">
<StackLayout>
<controls:TestControl />
</StackLayout>
</ContentPage>

扩展:

因为这些方法都是自己在平台自定义渲染的,所以可以很方便的监控原生事件,例如长按,双击等等,只需要基本了解一下安卓和苹果的东西,再结合上述的例子,就很容易扩展了

git项目地址: https://github.com/weiweu/TestProject/tree/dev

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,490
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,905
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,738
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,491
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,129
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,292