[英]How to implement Share Extension to Xamarin.Forms - iOS project?
[英]How to implement long press in Xamarin Forms for iOS?
我需要在Xamarin Forms for iOS中实现长按,并且找不到我需要的帖子。 我的工作代码如下。 希望它可以帮助某人。
我的自定义类ImgButton继承自Grid。 在其他情况下,您只需要根据此[table]将ViewRenderer替换为另一个渲染器。[1]
因为我想长按仅在某些情况下启用,ImgButton有一个属性EnableLongPress。
using System;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using UIKit;
[assembly: ExportRenderer (typeof(ImgButton), typeof(ImgButtonRenderer))]
namespace MyApp.iOS.Renderers
{
public class ImgButtonRenderer : ViewRenderer<ImgButton,ImgButtonRenderer>
{
private UILongPressGestureRecognizer longPressGestureRecognizer;
protected override void OnElementChanged ( ElementChangedEventArgs<ImgButton> e )
{
base.OnElementChanged ( e );
if ( e.NewElement != null )
{
if ( ! e.NewElement.EnableLongPress )
return;
Action longPressAction = new Action ( () =>
{
if ( longPressGestureRecognizer.State != UIGestureRecognizerState.Began )
return;
Console.WriteLine ( "Long press for " + e.NewElement.Text );
// Handle the long press in the PCL
e.NewElement.OnLongPress ( e.NewElement );
});
longPressGestureRecognizer = new UILongPressGestureRecognizer ( longPressAction );
longPressGestureRecognizer.MinimumPressDuration = 0.5D;
AddGestureRecognizer ( longPressGestureRecognizer );
}
if ( e.NewElement == null )
{
if ( longPressGestureRecognizer != null )
{
RemoveGestureRecognizer ( longPressGestureRecognizer );
}
}
if ( e.OldElement == null )
{
if ( longPressGestureRecognizer != null )
AddGestureRecognizer ( longPressGestureRecognizer );
}
}
}
在ImgButton类中:
public void OnLongPress ( ImgButton button )
// Here when a long press happens on an ImgButton
{
// Inform current page
MessagingCenter.Send<ImgButton, ImgButton> ( this, "LongPressMessageType", button );
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.