Xamarin.forms 如何获得发生的每一个pann手势
我有一个很简单的问题,但我找不到任何答案 我有一个应用程序,我想在每次出现平移/滑动/滑动手势时调用一个函数 我知道布局是如何组织的,所以这意味着我不能在主布局上添加一个手势,但同时,将这个手势添加到每个元素似乎令人毛骨悚然 有什么想法吗Xamarin.forms 如何获得发生的每一个pann手势,xamarin.forms,swipe,slide,pan,Xamarin.forms,Swipe,Slide,Pan,我有一个很简单的问题,但我找不到任何答案 我有一个应用程序,我想在每次出现平移/滑动/滑动手势时调用一个函数 我知道布局是如何组织的,所以这意味着我不能在主布局上添加一个手势,但同时,将这个手势添加到每个元素似乎令人毛骨悚然 有什么想法吗 感谢制作自定义页面渲染器并在那里添加手势 下面是一个关于iOS的例子 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Fo
感谢制作自定义页面渲染器并在那里添加手势 下面是一个关于iOS的例子
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Foundation;
using UIKit;
using Xamarin.Forms.Platform.iOS;
using Xamarin.Forms;
using Knok;
using Knok.iOS;
[assembly: ExportRenderer(typeof(SearchEventsPage), typeof(SearchEventsPageRenderer))]
namespace Knok.iOS
{
class SearchEventsPageRenderer : PageRenderer
{
UISwipeGestureRecognizer swipeLeftGestureRecognizer, swipeRightGestureRecognizer;
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
if (e.OldElement != null)
{
if (swipeLeftGestureRecognizer != null)
NativeView.RemoveGestureRecognizer(swipeLeftGestureRecognizer);
if (swipeRightGestureRecognizer != null)
NativeView.RemoveGestureRecognizer(swipeRightGestureRecognizer);
}
if (e.NewElement != null)
{
swipeLeftGestureRecognizer = new UISwipeGestureRecognizer(_ =>
{
((SearchEventsPage)Element).OnSwipeLeft();
})
{
Direction = UISwipeGestureRecognizerDirection.Left
};
NativeView.AddGestureRecognizer(swipeLeftGestureRecognizer);
swipeRightGestureRecognizer = new UISwipeGestureRecognizer(() =>
{
((SearchEventsPage)Element).OnSwipeRight();
})
{
Direction = UISwipeGestureRecognizerDirection.Right
};
NativeView.AddGestureRecognizer(swipeRightGestureRecognizer);
}
}
}
}
其中,在SearchEventsPage中将OnSwipeLeft和OnSwipeRight定义为
public async void OnSwipeLeft()
{
}
我发现一些不那么糟糕的东西,它也可以引发事件或调用给定的
操作
就这样用吧
<ContentPage.Content>
<AbsoluteLayout BackgroundColor="Black">
<component:PanContainer AbsoluteLayout.LayoutBounds="0.5, 0.5, 1, 1" AbsoluteLayout.LayoutFlags="All">
</component:PanContainer>
</AbsoluteLayout>
</ContentPage.Content>
不要忘记包括xmlns:component=“clr namespace:namespace.Sources.Components;assembly=NightLine”
如果你想引发一个事件或调用一个函数,可以从
private void OnPanUpdated(object sender,PanUpdatedEventArgs e){..}
我有了这个想法并开始做,但是,似乎要做很多工作,最终没有那么复杂,不是吗?很好,看起来他们添加了更多的手势识别器。上次我这么做时,他们只有触摸/点击识别器。
<ContentPage.Content>
<AbsoluteLayout BackgroundColor="Black">
<component:PanContainer AbsoluteLayout.LayoutBounds="0.5, 0.5, 1, 1" AbsoluteLayout.LayoutFlags="All">
</component:PanContainer>
</AbsoluteLayout>
</ContentPage.Content>