有没有办法让一个控件允许我使用只有Xamarin.Forms控件捏和缩放.
我想在xamarin.forms(WebView或Image或任何其他)的任何控件中显示图像,并能够从应用程序中进行缩放.
最佳答案 截至本文发布时,无法使用纯内置Forms控件进行捏合/缩放.有一种方法可以实现这一点,但您必须为此实现本机渲染器.
我通过创建一个继承自Xamarin.Forms.ContentView – PanGestureContainer的类在我正在编写的应用程序中实现了这一点,该类具有诸如最小/最大触摸点数和要侦听的事件之类的属性.
在iOS项目中,我为我的视图创建了一个自定义渲染器,其中渲染器从视图中获取属性并挂钩触摸事件侦听器.
另外,我创建了一个可附加属性(也称为Behavior),可以应用于其他视图,当应用它时,它从其父视图中获取视图,将其包装在PanGestureRecognizer中,另一个附加属性以相同的方式充当事件侦听器包装器.
这是一个完整的黑客,但涵盖了缺少的功能,直到Xamarin干净地实现它
更新:现在使用示例代码,严重修剪,因为它会发布太多,它应该让您知道如何实现这一点,而不是复制/粘贴解决方案.如果它看起来似乎太多了,我肯定有更好的方法,但它会成功,直到这个功能被烘焙.
public abstract class BaseInteractiveGestureRecognizer : BindableObject, IInteractiveGestureRecognizer
{
public static readonly BindableProperty CommandProperty = BindableProperty.Create<BaseInteractiveGestureRecognizer, ICommand> ((b) => b.Command, null, BindingMode.OneWay, null, null, null, null);
public ICommand Command {
get {
return (ICommand)base.GetValue (BaseInteractiveGestureRecognizer.CommandProperty);
}
set {
base.SetValue (BaseInteractiveGestureRecognizer.CommandProperty, value);
}
}
public object CommandParameter {get;set;} // make bindable as above
public GestureState State { get;set;} // make bindable as above
public View SourceView{ get; set; }
public void Send ()
{
if (Command != null && Command.CanExecute (this)) {
Command.Execute (this);
}
}
}
public class PanGesture : BaseInteractiveGestureRecognizer
{
public uint MinTouches { get;set; } // make bindable
public uint MaxTouches { get;set; } // make bindable
// add whatever other properties you need here - starting point, end point, touch count, current touch points etc.
}
然后在iOS项目中:
public abstract class BaseInteractiveGestureRenderer : BindableObject,IGestureCreator<UIView>
{
public abstract object Create (IInteractiveGestureRecognizer gesture, Element formsView, UIView nativeView);
public static GestureState FromUIGestureState (UIGestureRecognizerState state)
{
switch (state) {
case UIGestureRecognizerState.Possible:
return GestureState.Possible;
case UIGestureRecognizerState.Began:
return GestureState.Began;
case UIGestureRecognizerState.Changed:
return GestureState.Update;
case UIGestureRecognizerState.Ended:
return GestureState.Ended;
case UIGestureRecognizerState.Cancelled:
return GestureState.Cancelled;
case UIGestureRecognizerState.Failed:
return GestureState.Failed;
default:
return GestureState.Failed;
}
}
}
using StatementsHere;
[assembly: ExportGesture (typeof(PanGesture), typeof(PanGestureRenderer))]
namespace YourNamespaceHere.iOS
{
public class PanGestureRenderer : BaseInteractiveGestureRenderer
{
public PanGestureRenderer () : base ()
{
}
#region IGestureCreator implementation
public override object Create (IInteractiveGestureRecognizer gesture, Element formsView, UIView nativeView)
{
PanGesture panGesture = gesture as PanGesture;
nativeView.UserInteractionEnabled = true;
UIPanGestureRecognizer panGestureRecognizer = null;
panGestureRecognizer = new UIPanGestureRecognizer (() => panGesture.Send());
}
}