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. ...
随机推荐
- 527D.Clique Problem
题解: 水题 两种做法: 1.我的 我们假设$xi>xj$ 那么拆开绝对值 $$xi-w[i]>x[j]+w[j]$$ 由于$w[i]>0$,所以$x[i]+w[i]>x[j] ...
- 【spring】-- 手写一个最简单的IOC框架
1.什么是springIOC IOC就是把每一个bean(实体类)与bean(实体了)之间的关系交给第三方容器进行管理. 如果我们手写一个最最简单的IOC,最终效果是怎样呢? xml配置: <b ...
- port bridge enable命令导致的环路
1.故障描述 前几天机房一台连接数据中心与核心交换的交换机宕机(硬件故障),机房有备用的设备,随即更换(配置也是早就配置好了的),但是下午就出现数据中心网络丢包问题,表现为存在mac漂移 2.拓扑 核 ...
- 使用第三方插件Gear Tacks 画齿轮
以下介绍第二种方法: 重复再生成一个大的齿轮 两个都保存起来: 再创建一个装配体环境. 接下来就是要达到使两个轮子配合转起来的效果! 步骤如下:
- Telephone Lines [POJ3662] [二分答案]
Description Farmer John打算将电话线引到自己的农场,但电信公司并不打算为他提供免费服务.于是,FJ必须为此向电信公司支付一定的费用. FJ的农场周围分布着N(1 <= N ...
- Node.js_Buffer 缓冲区
Buffer 缓冲区 虽然 JavaScript 支持未操作,但是并没有 二进制数据 的原生 node 引入了 Buffer 类,用于操作二进制数据 是 V8 引擎的扩展,实际上是对内存的直接分配 每 ...
- [LeetCode] Maximum Depth of N-ary Tree N叉树的最大深度
Given a n-ary tree, find its maximum depth. The maximum depth is the number of nodes along the longe ...
- Codeforces Round #509 (Div. 2)
咕咕咕了好多天终于有时间写篇博客了_(:з」∠)_ 打网赛打到自闭的一周,终于靠这场CF找回了一点信心... 1041A - Heist \(ans=max\left \{ a_i \right \} ...
- 使用Java Low Level REST Client操作elasticsearch
Java REST客户端有两种风格: Java低级别REST客户端(Java Low Level REST Client,以后都简称低级客户端算了,难得码字):Elasticsearch的官方low- ...
- css 图片置灰
.filterGray{ -webkit-filter: grayscale(100%); -moz-filter: grayscale(100%); -ms-filter: grayscale(10 ...