如何在 Xamarin.Forms 和 ScnViewGestures.Forms 中使用 TouchDown 和 TouchUp 事件创建视图
How to create a view with TouchDown and TouchUp events in Xamarin.Forms with ScnViewGestures.Forms
ScnViewGestures.Forms 是一个 Nuget 包,它允许使用自定义触摸处理程序在页面中设置任何子视图。然而,通过阅读它在 github 1 上的 sparce 文档,要弄清楚如何使用它并不容易。那么如何在 Xamarin.Forms 中使用 TouchDown 和 TouchUp 事件处理程序创建自定义视图?
这是一个工作代码示例。不要忘记安装 nuget 包 ScnViewGestures.Forms 并像这样初始化它:
iOS(在AppDelegate.cs):
Xamarin.Forms.Forms.Init ();
ViewGesturesRenderer.Init();
Android:
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) {
}
}
}
ScnViewGestures.Forms 是一个 Nuget 包,它允许使用自定义触摸处理程序在页面中设置任何子视图。然而,通过阅读它在 github 1 上的 sparce 文档,要弄清楚如何使用它并不容易。那么如何在 Xamarin.Forms 中使用 TouchDown 和 TouchUp 事件处理程序创建自定义视图?
这是一个工作代码示例。不要忘记安装 nuget 包 ScnViewGestures.Forms 并像这样初始化它:
iOS(在AppDelegate.cs):
Xamarin.Forms.Forms.Init ();
ViewGesturesRenderer.Init();
Android:
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) {
}
}
}