提问:监控按钮的点击事件,可以通过按钮的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

  

菜鸟的Xamarin.Forms前行之路——按钮的按下抬起事件的监控(可扩展至其他事件)的更多相关文章

  1. 菜鸟的Xamarin.Forms前行之路——实现按钮的字体图标(可扩展)

    在实际的APP中,带有图标的按钮用到地方还是蛮多的,字体图标往往能更快更生动的传达信息,并且相对于背景图片,字体图标也有着绝对的优势,所以实现按钮的字体图标是值得尝试的. 实现方法:各平台自定义渲染按 ...

  2. 菜鸟的Xamarin.Forms前行之路——原生Toast的简单实现方法

    项目中信息提示框,貌似只有个DisplayAlert,信息提示太过于单一,且在有些场合Toast更加实用,以下是一个简单的原生Toast的实现方法 项目地址:https://github.com/we ...

  3. 菜鸟的Xamarin.Forms前行之路——绪言

    作者入门时间不是很久,差不多一年,期间自学的东西比较杂乱,到目前为止,编程方面的知识比较薄弱.之所以做这个系列,也只是因为做了两个月的Xamarin.Forms方面的东西,由于资料和自身实力的原因,过 ...

  4. 菜鸟的Xamarin.Forms前行之路——windows下VS运行ios模拟器调试

    在Xamarin.Forms项目中,运行安卓模拟器是很方便的,但是想要运行IOS模拟器,相对而言是困难一点. 在参考一些资料后,发现很多是与Xamarin.studio有关的方法,尝试了许久没有成功. ...

  5. 菜鸟的Xamarin.Forms前行之路——从新建项目到APP上架各种报错问题解决方法合集(不定时更新)

    出自:博客园-半路独行 原文地址:http://www.cnblogs.com/banluduxing/p/7425791.html 本文出自于http://www.cnblogs.com/banlu ...

  6. 菜鸟的Xamarin.Forms前行之路——共享组件

    出自:博客园-半路独行 本文出自于http://www.cnblogs.com/banluduxing 转载请注明出处. Url Description Xamarin.Social The Xama ...

  7. Xamarin.Forms ListView点击按钮刷新最新数据

    最近在研究Xamarin的东西,做到ListView遇到了一些瓶颈,像在数据庞大的情况下,该怎么针对ListView中的数据分组呢? 基于能力有限的问题,暂时写了一个只可以实现功能的临时解决方案,毕竟 ...

  8. Xamarin.Forms——WebView技术研究

    在Xamarin中有一些Forms原生不太好实现的内容可以考虑使用HTML.Javascript.CSS那一套前端技术来实现,使用WebView来承载显示本地或网络上的HTML文件.不像OpenUri ...

  9. Xamarin.Forms 学习系列之优秀UI收集

    1.各种优秀UI例子 https://github.com/jsuarezruiz/xamarin-forms-goodlooking-UI 输入框例子 https://github.com/enis ...

随机推荐

  1. Linux最小化安装

    1,linux安装网络自动配置: 2,linux硬盘分配 1,/boot 用来存放与 Linux 系统启动有关的程序,比如启动引导装载程序等,建议大小为 100-200MB . 2,swap 实现虚拟 ...

  2. H5读取本地文件操作

    H5读取本地文件操作 本文转自:转:http://hushicai.com/2014/03/29/html5-du-qu-ben-di-wen-jian.html感谢大神分享. 常见的语言比如php. ...

  3. docker - 修改镜像/容器文件的在宿主机上的存储位置(转)

    背景 之前在使用docker的时候,由于启动container的时候用的是默认的mount(路径为 /var/lib/docker),这个目录对应的硬盘空间有限,只有200G左右.现在随着程序运行,有 ...

  4. xdu_1009: Josephus环的复仇(线段树)

    题目链接 题意不难理解,解法具体看代码及注释吧.. #include<bits/stdc++.h> using namespace std; typedef long long LL; ; ...

  5. 第2章 rsync(二):inotify+rsync详细说明和sersync

    本文目录: inotify+rsync 1.1 安装inotify-tools 1.2 inotifywait命令以及事件分析 1.3 inotify应该装在哪里 1.4 inotify+rsync示 ...

  6. 从零开始搭建Jenkins+Docker自动化集成环境

    本文只简单标记下大概的步骤,具体搭建各个部分的细节,还请自行搜索.第一.二部分只是对Jenkins和Docker的简单介绍,熟悉的同学请直接跳到第三部分. 一.关于Jenkins Jenkins简介 ...

  7. C语言基础 - 实现单向链表

    回归C基础 实现一个单向链表,并有逆序功能 (大学数据结构经常是这么入门的) //定义单链表结构体 typedef struct Node{ int value; struct Node *next; ...

  8. Yii框架用ajax提交表单时候报错Bad Request (#400): Unable to verify your data submission.

    提交表单报400错误,提示 "您提交的数据无法验证"原来是csrf验证的问题,因为表单是自己写的,在Yii框架中,为了防止csrf攻击,对post的表单数据封装了CSRF令牌验证. ...

  9. 分布式事务,EventBus 解决方案:CAP【中文文档】

    前言 很多同学想对CAP的机制以及用法等想有一个详细的了解,所以花了将近两周时间写了这份中文的CAP文档,对 CAP 还不知道的同学可以先看一下这篇文章. 本文档为 CAP 文献(Wiki),本文献同 ...

  10. expdp导出文件,ORA-01555: 快照过旧: 回退段号 716

    快照号过旧,回退段号过小,信息如下:ORA-31693: 表数据对象 "CZBSDB"."SMS_RESULT_RECORD" 无法加载/卸载并且被跳过, 错误 ...