Xamarin.Forms 自定义 TapGestureRecognizer 附加属性
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 附加属性的更多相关文章
- Xamarin.forms 自定义tabview控件
一 问题描述 forms本身ui代码是翻译为平台原生代码,forms按照xaml技术进行对android和ios两种ui模型进行公共抽象出了几种page和view,在空杯博客已经有详细介绍 http: ...
- Xamarin.forms 自定义dropdownview控件
一 基本说明 想用xamarin做个像美团这样的下拉列表进行条件选择的功能,但是但是找了半天好像没有现成的,也没有其他类似的控件可以走走捷径,再则也没有找到popwindow之类的东东,这里只好使用s ...
- Xamarin.Forms 自定义控件(呈现器和效果)
Xamarin.Forms 使用目标平台的本机控件呈现用户界面,从而让 Xamarin.Forms 应用程序为每个平台保留了相应的界面外观.凭借效果,无需进行自定义呈现器实现,即可自定义每个平台上的本 ...
- Xamarin.Forms 调用 腾讯地图SDK
Xamarin.Forms研究了好一段时间了,最近一直在学习中,想尝试一下调用其他的SDK,就如腾讯地图SDK(申请容易). 完成此次项目得感谢以下链接: http://www.cnblogs.com ...
- 自定义xamarin.forms Entry 背景色以及边框
创建 一个Xamarin.Forms自定义控件. 自定义Entry控件可以通过继承来创建Entry控制,显示在下面的代码示例: public class MyEntry : Entry { ...
- Xamarin.Forms之XAML
官网参考 XAML基础知识 XAML(eXtensible Application Markup Language)可扩展应用程序标记语言,允许开发者在Xamarin.Forms应用中采用标记而不是代 ...
- 使用MvvmCross框架实现Xamarin.Forms的汉堡菜单布局
注:本文是英文写的,偷懒自动翻译过来了,原文地址:Implementing MasterDetail layout in Xamarin.Forms by MvvmCross 欢迎大家关注我的公众号: ...
- Xamarin.Forms移动开发系列4 :XAML基础
摘要 本文介绍Xamarin.Forms创建用户界面的语言:XAML基础部分. 前言 本文介绍Xamarin.Forms定义用户界面的语言:XAML. 本篇篇幅较长,主要讲述XAML语法,以及对其他基 ...
- Xamarin.Forms一些常见问题
安装 1.查看Xaramin.Forms的版本 在vs项目中查看引用的包(Xamarin.Forms)的版本,或者直接进文件夹看 C:\Microsoft\Xamarin\NuGet\xamarin. ...
随机推荐
- thread - 传递引用参数
当给 thread 的执行函数传递指针参数时,没有任何问题,但是如果想传递引用,按照普通函数的调用方法会遇到编译失败: #include <iostream> #include <t ...
- MVC中 jquery validate 不用submit方式验证表单或单个元素
<script src="/Scripts/jquery-1.4.4.js"></script> <script src="/Scripts ...
- Kafka集群搭建 (2.11-0.9.0.1)
之前写过kafka_2.9.2-0.8.2.2版本的安装,kafka在新的0.9版本以上改动比较大,配置和api都有很大更新,并且broker对应的partition支持多线程生产和消费,所以性能比之 ...
- LNMP一键安装包添加虚拟主机、删除虚拟主机及如何使用伪静态
本文主要介绍LNMP一键安装包添加虚拟主机.删除虚拟主机及如何使用伪静态. 一.添加虚拟主机通俗点就是在VPS/服务商上添加一个网站(域名). 需要执行如下命令:/root/vhost.sh 执行后会 ...
- linux服务器文件授权命令
分两部分改属主和属组权限:更改权限,递归方式 chmod -R 755 /var/www/html/test.com更改属主,递归chown -R apache:apache /var/www/htm ...
- Spring异步调用原理及SpringAop拦截器链原理
一.Spring异步调用底层原理 开启异步调用只需一个注解@EnableAsync @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTI ...
- JAVA篇<一> 继承extends(已转移到JAVA总结篇)
前题:如果不经过指出继承,那么所有的类都继承了JAVA中的Object类. 正文: 继承的关键字是:extends,是所有面向对象语言的重要特性. 例public class TestExtends ...
- 二分三元组 CodeForces - 251A
题目链接: https://vjudge.net/problem/35188/origin 题目大意: 要求你找到一个 i < j < k时有 a[k]-a[i] <= d的组的个数 ...
- BZOJ.2780.[SPOJ8093]Sevenk Love Oimaster(广义后缀自动机)
题目链接 \(Description\) 给定n个模式串,多次询问一个串在多少个模式串中出现过.(字符集为26个小写字母) \(Solution\) 对每个询问串进行匹配最终会达到一个节点,我们需要得 ...
- Mybatis_1(认识)一个简单的HelloWorld
1. 介绍: MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架. MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索. MyBatis可以使用简单的XM ...