gpt4 book ai didi

c# - Xamarin UISwipeGestureRecognizer 渲染器

转载 作者:行者123 更新时间:2023-11-30 14:53:12 25 4
gpt4 key购买 nike

在 Xamarin.Forms 中,我希望能够左右轻扫以导航图片列表。目前,我只想在每次检测到滑动时触发一个事件。

要在渲染器中使用的子类:

public class LRMasterDetailPage : ContentView
{
}

我有一个使用 LRM 类的 ContentPage,如下所示:

    public class ImagePage : ContentPage 
{

public ImagePage(Photo photo)
{
_image = new WebImage
{
Url = photo.Url,
Placeholder = "placeHolder2.png"
};

var imageView = new LRMasterDetailPage {
Content = _image
};

this.Content = imageView;
}
}

这是我的渲染器:

    [assembly:ExportRenderer (typeof(LRMasterDetailPage), typeof(LRMDPRenderer))]
namespace Project.iOS
{
public class LRMDPRenderer : ViewRenderer<LRMasterDetailPage,UIView>
{
UISwipeGestureRecognizer swipe;

protected override void OnElementChanged (ElementChangedEventArgs<LRMasterDetailPage> e)
{
base.OnElementChanged (e);
// Do someting else, init for example
swipe = new UISwipeGestureRecognizer();
this.AddGestureRecognizer (swipe);

if (swipe.Direction == UISwipeGestureRecognizerDirection.Left)
{
UpdateLeft ();
}

if (swipe.Direction == UISwipeGestureRecognizerDirection.Right)
{
UpdateRight ();
}
}

protected override void OnElementPropertyChanged (object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == "Renderer")
return;
base.OnElementPropertyChanged (sender, e);

}

private void UpdateLeft(){
// Insert view of DetailLeft element into subview
// Add button to open Detail to parent navbar, if not yet there
// Add gesture recognizer for left swipe
Console.WriteLine ("Left swipe");

}
private void UpdateRight(){
// same as for left, but flipped
Console.WriteLine ("Right swipe");

}
}
}

当显示 ContentPage 时,向右滑动事件被激活,但当我尝试在图像顶部滑动时没有任何反应。我猜我在渲染器上的逻辑是错误的?

最佳答案

在经历了很多痛苦并在网上搜索之后,我找到了解决方案。

您需要做的很简单:在渲染器中声明并添加手势。确保通过声明它们的方向为左右分别创建滑动手势。从那里使用 lambda 调用要为特定滑动激活的功能:

滑动手势的占位符类。

public class LRMasterDetailPage : ContentView
{

}

一次保存一张图片的图片页

public class ImagePage : ContentPage
{

//view holding the image
LRMasterDetailPage imageView;

//collection of images using the photo.Url
ObservableCollection<Image> images;

//current image index
int index = 0;

public ImagePage(){

images = new ObservableCollection<Image> ();
imageView = new LRMasterDetailPage {
Content = this.images [index]
};

this.Content = imageView;
}

//Subscribe to the swipeLeft and swipeRight message
protected override void OnAppearing ()
{
base.OnAppearing ();

MessagingCenter.Subscribe <string> (this,"LeftSwipe", (sender) => {

//Do something
if(index < images.Count-1){
index++;
}

imageView.Content = this.images[index];


});
MessagingCenter.Subscribe <string> (this, "RightSwipe", (sender) => {

//Do something
if(index > 0){
index--;
}

imageView.Content = this.images[index];


});
}

protected override void OnDisappearing()
{
base.OnDisappearing();

//this._image = null;

images = null;

MessagingCenter.Unsubscribe<string>(this,"LeftSwipe");
MessagingCenter.Unsubscribe<string>(this, "RightSwipe");
MessagingCenter.Unsubscribe<string>(this, "LongPress");

//GC.Collect();
}

}

LRMasterDetailPage 渲染器

    [assembly:ExportRenderer (typeof(LRMasterDetailPage), typeof(LRMDPRenderer))]
namespace Manager.iOS
{
public class LRMDPRenderer : ViewRenderer<LRMasterDetailPage,UIView>
{

UILongPressGestureRecognizer longPressGestureRecognizer;
UIPinchGestureRecognizer pinchGestureRecognizer;
//UIPanGestureRecognizer panGestureRecognizer;
UISwipeGestureRecognizer swipeRightGestureRecognizer;
UISwipeGestureRecognizer swipeLeftGestureRecognizer;
UIRotationGestureRecognizer rotationGestureRecognizer;

protected override void OnElementChanged (ElementChangedEventArgs<LRMasterDetailPage> e)
{
base.OnElementChanged (e);

longPressGestureRecognizer = new UILongPressGestureRecognizer (() => Console.WriteLine ("Long Press"));
pinchGestureRecognizer = new UIPinchGestureRecognizer (() => Console.WriteLine ("Pinch"));
//panGestureRecognizer = new UIPanGestureRecognizer (() => Console.WriteLine ("Pan"));

swipeRightGestureRecognizer = new UISwipeGestureRecognizer ( () => UpdateRight()){Direction = UISwipeGestureRecognizerDirection.Right};
swipeLeftGestureRecognizer = new UISwipeGestureRecognizer ( () => UpdateLeft()){Direction = UISwipeGestureRecognizerDirection.Left};
rotationGestureRecognizer = new UIRotationGestureRecognizer (() => Console.WriteLine ("Rotation"));

if (e.NewElement == null) {
if (longPressGestureRecognizer != null) {
this.RemoveGestureRecognizer (longPressGestureRecognizer);
}
if (pinchGestureRecognizer != null) {
this.RemoveGestureRecognizer (pinchGestureRecognizer);
}

/*
if (panGestureRecognizer != null) {
this.RemoveGestureRecognizer (panGestureRecognizer);
}
*/

if (swipeRightGestureRecognizer != null) {
this.RemoveGestureRecognizer (swipeRightGestureRecognizer);
}
if (swipeLeftGestureRecognizer != null) {
this.RemoveGestureRecognizer (swipeLeftGestureRecognizer);
}

if (rotationGestureRecognizer != null) {
this.RemoveGestureRecognizer (rotationGestureRecognizer);
}
}

if (e.OldElement == null) {
this.AddGestureRecognizer (longPressGestureRecognizer);
this.AddGestureRecognizer (pinchGestureRecognizer);
//this.AddGestureRecognizer (panGestureRecognizer);
this.AddGestureRecognizer (swipeRightGestureRecognizer);
this.AddGestureRecognizer (swipeLeftGestureRecognizer);
this.AddGestureRecognizer (rotationGestureRecognizer);
}
}

private void UpdateLeft(){
MessagingCenter.Send ("Swiped to the left", "LeftSwipe");

}
private void UpdateRight(){
// same as for left, but flipped
MessagingCenter.Send ("Swiped to the Right", "RightSwipe");

}
}
}

关于c# - Xamarin UISwipeGestureRecognizer 渲染器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29972220/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com