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. ...
随机推荐
- CentOS7上RabbitMQ安装
因为RabbitMQ是由erlang实现的,所以要先安装erlang再安装rabbitMQ 一.配置yum软件源地址EPEL(EPEL是管理yum下载软件的软件,也可以说是一个软件仓库)后安装erla ...
- 【BZOJ4589】Hard Nim(FWT)
题解: 由博弈论可以知道题目等价于求这$n$个数$\^$为0 快速幂$+fwt$ 这样是$nlog^2$的 并不能过 而且得注意$m$的数组$\^$一下会生成$2m$ #include <bit ...
- 企业级Docker-Harbor
[环境准备] # yum install -y yum-utils device-mapper-persistent-data lvm2 下载docker-ce版本的yum源 # yum-config ...
- 把.Net开发环境迁移到Linux上去
.Net Core发布之前,多年来,.Net程序员的开发环境都在Windows上. 三街第一帅的我,虽然上班的8小时一直在windows上撸C#,但是下班时间一般都在搞其他的乱七八糟的东西,比如写写小 ...
- python实现的跳点寻路算法(JPS)
原理参考论文 代码已提交到git(https://github.com/YYRise/find_path/blob/master/jps.py)
- nginx Provisional headers are shown
项目用的Nginx做的代理,重启电脑后,重启项目和Nginx 浏览器报 Provisional headers are shown 解决: host文件添加: 127.0.0.1 cleaner ...
- 小甲鱼Python第二十一讲课后习题
测试题: 0. 递归在编程上的形式是如何表现的呢? 在编程上,递归表现为函数调用本身这么一个行为. 1. 递归必须满足哪两个基本条件? 一. 函数调用自身二. 设置了正 ...
- Angularjs判断页面是否已经渲染结束(动态给标签长度)
相信大家都会碰到这样的问题.页面循环li.但是因为个数不知道.没有办法给li设置固定宽度.那么这时就需要动态计算数据长度并动态改变li的宽度 <!--周边信息--> <div cla ...
- python +ps 三方面库整理
-------------------------------------------端口进程相关------------------------------------------------cp ...
- VBA - ONE
1. Grouping our code (1) Modules