Android 如何使用ScnViewGestures.Forms在Xamarin.Forms中创建具有触地和触地事件的视图
Forms是一个Nuget软件包,允许使用自定义触摸处理程序在页面中设置任何子视图。然而,通过阅读github上的sparce docu,不容易理解如何使用它。那么,如何使用Xamarin.Forms中的触地和触地事件处理程序创建自定义视图呢?下面是一个工作代码示例。别忘了安装nuget软件包ScnViewGestures.Forms,并按如下方式初始化它: iOS(在AppDelegate.cs中): 安卓:Android 如何使用ScnViewGestures.Forms在Xamarin.Forms中创建具有触地和触地事件的视图,android,ios,xamarin,xamarin.forms,Android,Ios,Xamarin,Xamarin.forms,Forms是一个Nuget软件包,允许使用自定义触摸处理程序在页面中设置任何子视图。然而,通过阅读github上的sparce docu,不容易理解如何使用它。那么,如何使用Xamarin.Forms中的触地和触地事件处理程序创建自定义视图呢?下面是一个工作代码示例。别忘了安装nuget软件包ScnViewGestures.Forms,并按如下方式初始化它: iOS(在AppDelegate.cs中): 安卓: Xamarin.Forms.Forms.Init (this, bundle); V
Xamarin.Forms.Forms.Init (this, bundle);
ViewGesturesRenderer.Init();
WinPhone:
Xamarin.Forms.Forms.Init ();
ViewGesturesRenderer.Init();
然后您可以创建像这个小按钮一样的自定义视图,它分别处理向下和向上事件,这是Xamarin.Forms标准按钮无法做到的
using System;
using System.Collections.Generic;
using Xamarin.Forms;
using ScnViewGestures.Plugin.Forms;
namespace ViewGesturesExample {
public partial class ButtonView : ContentView
{
public ButtonView ()
{
InitializeComponent ();
Content = _gestures = new ViewGestures () {
HorizontalOptions = new LayoutOptions(LayoutAlignment.Fill, true),
VerticalOptions = new LayoutOptions(LayoutAlignment.Fill, true),
};
_gestures.Content=_button=new Label () {
HorizontalOptions = new LayoutOptions(LayoutAlignment.Fill, true),
VerticalOptions = new LayoutOptions(LayoutAlignment.Fill, true),
XAlign = TextAlignment.Center,
YAlign = TextAlignment.Center,
};
_gestures.TouchBegan += OnTouchDown;
_gestures.TouchEnded += OnTouchUp;
// here you have more events like
//Tap;
//SwipeLeft;
//SwipeRight;
//SwipeUp;
//SwipeDown.
}
Label _button;
ViewGestures _gestures;
private void OnTouchDown(object sender, EventArgs args) {
}
private void OnTouchUp(object sender, EventArgs args) {
}
}
}
对于所有寻找所用Plugin源代码的人:
using System;
using System.Collections.Generic;
using Xamarin.Forms;
using ScnViewGestures.Plugin.Forms;
namespace ViewGesturesExample {
public partial class ButtonView : ContentView
{
public ButtonView ()
{
InitializeComponent ();
Content = _gestures = new ViewGestures () {
HorizontalOptions = new LayoutOptions(LayoutAlignment.Fill, true),
VerticalOptions = new LayoutOptions(LayoutAlignment.Fill, true),
};
_gestures.Content=_button=new Label () {
HorizontalOptions = new LayoutOptions(LayoutAlignment.Fill, true),
VerticalOptions = new LayoutOptions(LayoutAlignment.Fill, true),
XAlign = TextAlignment.Center,
YAlign = TextAlignment.Center,
};
_gestures.TouchBegan += OnTouchDown;
_gestures.TouchEnded += OnTouchUp;
// here you have more events like
//Tap;
//SwipeLeft;
//SwipeRight;
//SwipeUp;
//SwipeDown.
}
Label _button;
ViewGestures _gestures;
private void OnTouchDown(object sender, EventArgs args) {
}
private void OnTouchUp(object sender, EventArgs args) {
}
}
}