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:

1 <Image Source="img.png">
2   <Image.GestureRecognizers>
3     <TapGestureRecognizer Command="{Binding CloseCommand}" CommandParamter="{Binding .}" />
4   </Image.GestureRecognizers>
5 </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

 1 using System;
 2 using Xamarin.Forms;
 3 using Xamarin.Forms.Xaml;
 4 using System.Windows.Input;
 5
 6 namespace MyApp.Extensions
 7 {
 8     [ContentProperty("Command")]
 9     public class CommandExtension : BindableObject, IMarkupExtension
10     {
11         public string Command { get; set; }
12
13         // Binding source for Command
14         public object Source { get; set; }
15
16         // CommandParameter
17         public object Parameter { get; set; }
18
19         public object ProvideValue(IServiceProvider serviceProvider)
20         {
21             return this;
22         }
23     }
24 }
 1 using Xamarin.Forms;
 2 using System.Linq;
 3
 4 namespace MyApp.Extensions
 5 {
 6     public class Gestures
 7     {
 8         #region TapCommand
 9
10         public static readonly BindableProperty TapCommandProperty =
11             BindableProperty.CreateAttached<Gestures, CommandExtension>(
12               x => (CommandExtension)x.GetValue(Gestures.TapCommandProperty),
13               null, BindingMode.OneWay, propertyChanged: TapCommandChanged);
14
15         #endregion
16
17         private static void TapCommandChanged(BindableObject source, CommandExtension oldVal,
18                                               CommandExtension newVal)
19         {
20             var tapGesture = new TapGestureRecognizer();
21
22             // Set command
23             var commandBinding = new Binding(newVal.Command, source: newVal.Source);
24             tapGesture.SetBinding(TapGestureRecognizer.CommandProperty, commandBinding);
25
26             // Set command parameter
27             if (newVal.Parameter is Binding)
28             {
29                 tapGesture.SetBinding(TapGestureRecognizer.CommandParameterProperty,
30                                       newVal.Parameter as Binding);
31             }
32             else
33             {
34                 tapGesture.CommandParameter = newVal.Parameter;
35             }
36
37             // Remove old TapGestureRecognizer
38             var view = source as View;
39             var toRemove = view.GestureRecognizers.OfType<TapGestureRecognizer>().FirstOrDefault();
40             view.GestureRecognizers.Remove(toRemove);
41
42             // Add new one
43             view.GestureRecognizers.Add(tapGesture);
44         }
45     }
46 }

原文地址:https://www.cnblogs.com/mschen/p/10565231.html

时间: 2024-08-31 21:06:33

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

Xamarin.Forms自定义GridView

Xamarin.Forms自定义GridView 在开发中,我们经常用到以格子的形式来展示我们的数据,在很多平台的控件中我们叫做GridView, 在Xamarin.Forms中没有原生的GridView,这里简单介绍一种利用Xamarin.Forms中的Grid来实现 GridView的方法. 原理就是对Grid动态添加RowDefinition和ColumnDefinition. 代码如下: 1 using System; 2 using System.Collections; 3 usin

Xamarin.Forms自定义用户界面控件实现一个HybridWebView(混合webview)

原文:Implementing a HybridWebView呈现一个特定于平台的视图 Xamarin.Forms自定义用户界面控件应该来自视图类(View class),用于在屏幕上放置布局和控件.本文演示了如何为HybridWebView(混合webview)自定义控件创建自定义渲染器,该控件演示了如何增强特定平台的web控件,以允许从JavaScript调用c#代码. 每一个Xamarin.Forms视图为每个创建本地控件实例的平台提供了相应的渲染器.当一个视图被Xamarin.Forms

Xamarin.forms 自定义dropdownview控件

一 基本说明 想用xamarin做个像美团这样的下拉列表进行条件选择的功能,但是但是找了半天好像没有现成的,也没有其他类似的控件可以走走捷径,再则也没有找到popwindow之类的东东,这里只好使用stacklaout+contentview做一个伪下拉,因为没有fragement那样的控件,所以没有华丽丽的叠加层,也没有华丽丽的遮罩,由于xamarin的动画只发现了缩放,旋转,淡入淡出,位置变换(研究不深入,大神勿喷),暂时没有发现下拉一类的动画,所以暂时用缩放来展现下来的内容,使下拉的时候不

Xamarin.Forms 调用 腾讯地图SDK

Xamarin.Forms研究了好一段时间了,最近一直在学习中,想尝试一下调用其他的SDK,就如腾讯地图SDK(申请容易). 完成此次项目得感谢以下链接: http://www.cnblogs.com/jtang/p/4698496.html 其他文档参考: 腾讯地图SDK(安卓)文档 这里面有详细的使用过程(当然里面的代码是不适用C#的,不过要从这里下载SDK,也有如何申请Key的过程,请参考阅读) Xamarin.Forms自定义每个平台的控件文档 里面有如何根据不同的平台条件下,调用其他页

自定义xamarin.forms Entry 背景色

创建 一个Xamarin.Forms自定义控件. ? 自定义Entry控件可以通过继承来创建Entry控制,显示在下面的代码示例: public class MyEntry : Entry { } ? 消费 从Xamarin.Forms自定义控件. 该MyEntry控制可在XAML通过宣布其位置的命名空间,使用控制元素的命名空间前缀引用在PCL项目. 下面的代码示例显示了如何MyEntry控制可以通过一个XAML页面消耗: <ContentPage ... xmlns:local="clr

Xamarin.Forms TapGestureRecognizer 实现点击事件

Xamarin.Forms TapGestureRecognizer实现点击事件 在开发过程中使用点击事件是不可避免的,在Xamarin.Forms中Button有明确的点击(Clicked)事件, 但是很多其他的控件并没有点击事件,比如:Image.Label.这时我们该如何解决点击问题呢? 当然是GestureRecognizers啦,怎么使用呢,待我徐徐道来. Xaml代码: <Label Text="0"> <Label.GestureRecognizers&

让你的Xamarin.Forms应用程序访问

注意:在Xamarin.Forms 2.3.5-pre3发布版,我们将类名Accessibility变更为AutomationProperties. iOS.Android和Windows都公开了开发人员为每个人构建可访问的移动应用程序的API. 我们很高兴地宣布,我们已经添加了新的API访问Xamarin.Forms,使它更容易为开发人员构建访问,跨平台移动应用.在这篇文章中,我们将在新的API添加到Xamarin.Forms,使它容易建立访问的移动应用. Xamarin.Forms中的Aut

Xamarin.Forms之Effects的使用

在 Xamarin.Forms 2.1.0-pre1 ,Xamarin.Forms新增了新的Effects API. Effects是一系列方法,为了给View的渲染器添加运行时改变. 然而,我想强调的是, Effects天生被设计为高可复用的. 如果一个Effect能够解决一个难题, 它可能能够在你的整个APP中使用.如果你写了一个Effect来解决你的一个难题, 你能够分享它给其他遇到同样问题的人. 这篇文章尝试展示一种方式,能够帮助我们使分享Effects这件事变得很简单. public

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

提问:监控按钮的点击事件,可以通过按钮的Click事件,或者Command绑定,那么如何监控按钮的按下与抬起,或者移动,长按,双击等事件? 解决方法:各个平台自定义渲染依赖注入. 共享项目PCL: 1先定义一个继承Button的实体类NewButton.cs public class NewButton : Button { public event EventHandler Pressed; public event EventHandler Released; public virtual