While creating Xamarin.Forms applications, definitely you are going to need TapGestureRecognizer often. Implementing it in XAML many times may end up with a lot of unnecessary code. Let’s take a look at that simple clickable Image:

 <Image Source="img.png">
<Image.GestureRecognizers>
<TapGestureRecognizer Command="{Binding CloseCommand}" CommandParamter="{Binding .}" />
</Image.GestureRecognizers>
</Image>

This is a lot of lines, especially when you have to add many clickable controls. However, we can do it better and put everything into a single line using our custom attached property:

XHTML

xmlns:ex="clr-namespace:MyApp.Extensions;assembly=MyApp"
...
<Image Source="img.png" ex:Gestures.TapCommand="{ex:Command CloseCommand, Parameter={Binding .}}"/> xmlns:ex="clr-namespace:MyApp.Extensions;assembly=MyApp"
...
<Image Source="img.png" ex:Gestures.TapCommand="{ex:Command CloseCommand, Parameter={Binding .}}"/>

Implementation

 using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using System.Windows.Input; namespace MyApp.Extensions
{
[ContentProperty("Command")]
public class CommandExtension : BindableObject, IMarkupExtension
{
public string Command { get; set; } // Binding source for Command
public object Source { get; set; } // CommandParameter
public object Parameter { get; set; } public object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
}
 using Xamarin.Forms;
using System.Linq; namespace MyApp.Extensions
{
public class Gestures
{
#region TapCommand public static readonly BindableProperty TapCommandProperty =
BindableProperty.CreateAttached<Gestures, CommandExtension>(
x => (CommandExtension)x.GetValue(Gestures.TapCommandProperty),
null, BindingMode.OneWay, propertyChanged: TapCommandChanged); #endregion private static void TapCommandChanged(BindableObject source, CommandExtension oldVal,
CommandExtension newVal)
{
var tapGesture = new TapGestureRecognizer(); // Set command
var commandBinding = new Binding(newVal.Command, source: newVal.Source);
tapGesture.SetBinding(TapGestureRecognizer.CommandProperty, commandBinding); // Set command parameter
if (newVal.Parameter is Binding)
{
tapGesture.SetBinding(TapGestureRecognizer.CommandParameterProperty,
newVal.Parameter as Binding);
}
else
{
tapGesture.CommandParameter = newVal.Parameter;
} // Remove old TapGestureRecognizer
var view = source as View;
var toRemove = view.GestureRecognizers.OfType<TapGestureRecognizer>().FirstOrDefault();
view.GestureRecognizers.Remove(toRemove); // Add new one
view.GestureRecognizers.Add(tapGesture);
}
}
}

Xamarin.Forms 自定义 TapGestureRecognizer 附加属性的更多相关文章

  1. Xamarin.forms 自定义tabview控件

    一 问题描述 forms本身ui代码是翻译为平台原生代码,forms按照xaml技术进行对android和ios两种ui模型进行公共抽象出了几种page和view,在空杯博客已经有详细介绍 http: ...

  2. Xamarin.forms 自定义dropdownview控件

    一 基本说明 想用xamarin做个像美团这样的下拉列表进行条件选择的功能,但是但是找了半天好像没有现成的,也没有其他类似的控件可以走走捷径,再则也没有找到popwindow之类的东东,这里只好使用s ...

  3. Xamarin.Forms 自定义控件(呈现器和效果)

    Xamarin.Forms 使用目标平台的本机控件呈现用户界面,从而让 Xamarin.Forms 应用程序为每个平台保留了相应的界面外观.凭借效果,无需进行自定义呈现器实现,即可自定义每个平台上的本 ...

  4. Xamarin.Forms 调用 腾讯地图SDK

    Xamarin.Forms研究了好一段时间了,最近一直在学习中,想尝试一下调用其他的SDK,就如腾讯地图SDK(申请容易). 完成此次项目得感谢以下链接: http://www.cnblogs.com ...

  5. 自定义xamarin.forms Entry 背景色以及边框

    创建   一个Xamarin.Forms自定义控件.     自定义Entry控件可以通过继承来创建Entry控制,显示在下面的代码示例: public class MyEntry : Entry { ...

  6. Xamarin.Forms之XAML

    官网参考 XAML基础知识 XAML(eXtensible Application Markup Language)可扩展应用程序标记语言,允许开发者在Xamarin.Forms应用中采用标记而不是代 ...

  7. 使用MvvmCross框架实现Xamarin.Forms的汉堡菜单布局

    注:本文是英文写的,偷懒自动翻译过来了,原文地址:Implementing MasterDetail layout in Xamarin.Forms by MvvmCross 欢迎大家关注我的公众号: ...

  8. Xamarin.Forms移动开发系列4 :XAML基础

    摘要 本文介绍Xamarin.Forms创建用户界面的语言:XAML基础部分. 前言 本文介绍Xamarin.Forms定义用户界面的语言:XAML. 本篇篇幅较长,主要讲述XAML语法,以及对其他基 ...

  9. Xamarin.Forms一些常见问题

    安装 1.查看Xaramin.Forms的版本 在vs项目中查看引用的包(Xamarin.Forms)的版本,或者直接进文件夹看 C:\Microsoft\Xamarin\NuGet\xamarin. ...

随机推荐

  1. Mybatis内批量插入Oracle

    <insert id="insertManagerInfoBatch" parameterType="java.util.List"> <se ...

  2. 我是如何用redis做实时订阅推送的

    前阵子开发了公司领劵中心的项目,这个项目是以redis作为关键技术落地的.       先说一下领劵中心的项目吧,这个项目就类似京东app的领劵中心,当然图是截取京东的,公司的就不截了...   其中 ...

  3. Problem J. Journey with Pigs

    Problem J. Journey with Pigshttp://codeforces.com/gym/241680/problem/J考察排序不等式算出来单位重量在每个村庄的收益,然后生序排列猪 ...

  4. websocket-heartbeat-js心跳检测库正式发布

    前言: 两年前写了一篇websocket心跳的博客——初探和实现websocket心跳重连.  阅读量一直比较大,加上最近考虑写一个自己的npm包,因此就完成了一个websocket心跳的检测库.在这 ...

  5. web理论知识--网页访问过程(附有Django的web项目访问流程)

    当我们闲暇之余想上网看看新闻,或者看个电影,通常的操作是:打开电脑.打开浏览器.输入网址.浏览页面信息.点击自己感兴趣的连接......那么有没有想过,这些网页从哪里来的?过程中计算机又做了什么事情了 ...

  6. WinForm 水晶报表的简单使用

    今天需要做出一个水晶报表, 以前在学校的时候就看过一点点,有些印象, 但没有具体的了解过,今天百度了一下,发现这个东西相当的方便简单. 还很完美. 开发工具是VS2010,水晶报表没有内置.需要自己下 ...

  7. 【Codeforces】【网络流】【线段树】【扫描线】Oleg and chess (CodeForces - 793G)

    题意: 给定一个n*n的矩阵,一个格子上可以放一个车.其中有q个子矩阵,且q个子矩阵互不相交或者是重叠(但边界可以衔接).这q个子矩阵所覆盖的地方都是不能够放车的.车可以越过子矩阵覆盖的地方进行攻击( ...

  8. 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第5章编程练习4

    #include <iostream>using namespace std;const MAXSIZE=12;int main(){ char *month[MAXSIZE]={&quo ...

  9. react-native-splash-screen 插件 android 系统app崩溃问题

    问题 react-native版本 0.53.3 react-native-splash-screen版本 3.0.6 一切配置妥当后出现如下问题: 在android studio里的调试报错为and ...

  10. 运行make_datafiles的过程

    1. 第一个bug 运行 echo "Please tokenize this text." | java edu.stanford.nlp.process.PTBTokenize ...