Ios Xamarin.forms收缩和缩放

Ios Xamarin.forms收缩和缩放,ios,xamarin.forms,Ios,Xamarin.forms,有没有一种方法可以让我只使用Xamarin.Forms控件进行收缩和缩放 我想在xamarin.forms(WebView或image或任何其他)的任何控件中显示图像,并能够从应用程序中进行缩放。截至本文发布时,还没有办法使用纯内置的表单控件进行收缩/缩放。有一种方法可以实现这一点,但必须为此实现本机渲染器 我在编写的一个应用程序中实现了这一点,创建了一个继承自Xamarin.Forms.ContentView-PanGestureContainer的类,该类具有诸如最小/最大接触点数量和要侦听

有没有一种方法可以让我只使用Xamarin.Forms控件进行收缩和缩放


我想在xamarin.forms(WebView或image或任何其他)的任何控件中显示图像,并能够从应用程序中进行缩放。

截至本文发布时,还没有办法使用纯内置的表单控件进行收缩/缩放。有一种方法可以实现这一点,但必须为此实现本机渲染器

我在编写的一个应用程序中实现了这一点,创建了一个继承自Xamarin.Forms.ContentView-PanGestureContainer的类,该类具有诸如最小/最大接触点数量和要侦听的事件等属性

在iOS项目中,我为我的视图制作了一个自定义渲染器,其中渲染器从视图中获取属性并连接触摸事件侦听器

此外,我还制作了一个可附加属性(aka Behavior),可以应用于其他视图,当应用时,它从其父视图获取视图,将其包装在PangestureRecognitor中,另一个附加属性以相同的方式充当事件侦听器包装器

这是一个完整的破解,但在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.
}
公共抽象类BaseInteractiveGestereRecognitor:BindableObject,IIInteractiveGestereRecognitor
{
public static readonly BindableProperty CommandProperty=BindableProperty.Create((b)=>b.Command,null,BindingMode.OneWay,null,null,null,null);
公共ICommand命令{
得到{
return(ICommand)base.GetValue(baseInteractiveGetStureRecogniter.CommandProperty);
}
设置{
base.SetValue(baseInteractiveGesterRecogniter.CommandProperty,value);
}
}
公共对象CommandParameter{get;set;}//如上所述使可绑定
public GestureState状态{get;set;}//如上所述使可绑定
公共视图源视图{get;set;}
公共无效发送()
{ 
if(Command!=null&&Command.CanExecute(此)){
命令。执行(这个);
}
}
}
公共类泛测:BaseInteractiveGestereReceigner
{
public uint MinTouches{get;set;}//make bindable
public uint maxTouchs{get;set;}//使可绑定
//在此处添加您需要的任何其他属性-起点、终点、触摸计数、当前触摸点等。
}
然后在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());
    }
}
公共抽象类BaseInteractiveGetUserEnder:BindableObject,IGestureCreator
{
公共抽象对象创建(iInteractiveGestureRecognitor手势、元素表单视图、UIView nativeView);
来自UIGestureState的公共静态GestureState(UIGestureRecognitizerState)
{
开关(状态){
案例UIgestureRecognitzerState。可能:
返回手势状态。可能;
案例UIgestureRecognitizerState。开始时间:
返回手势状态。开始;
案例UIgestureRecognitizerState。已更改:
返回GestureState.Update;
案例UIgestureRecognitizerState。结束时间:
返回手势状态。结束;
案例UIgestureRecognitizerState。已取消:
返回手势状态。已取消;
案例UIgestureRecognitizerState。失败:
返回GestureState。失败;
违约:
返回GestureState。失败;
}    
}
}
使用statementsher;
[组件:导出手势(typeof(pangestrue)、typeof(pangestruerenderer))]
namespace YourNamespaceHere.iOS
{
公共类PanGestureRenderer:BaseInteractivateEstureRenderer
{
公共PanGestureRenderer():base()
{   
}
#区域igesturerator实现
公共覆盖对象创建(iInteractiveGestureRecognitor手势、元素表单视图、UIView nativeView)
{   
PanGesture PanGesture=作为PanGesture的手势;
nativeView.UserInteractionEnabled=true;
UIPangestureRecognitor PangestureRecognitor=null;
panGestureRecognizer=新的UIPanGestureRecognizer(()=>panGesture.Send());
}
}

我最终使用元视口进行缩放,如下所示。 这可能不是每个人的解决方案,但对我来说是有效的

将图像十六进制-64放在下面的图像标记中,然后将整个html放在WebView中

this.WebView.Source = new HtmlWebViewSource { BaseUrl = URL, Html = html };

The html will be defined here.
<!DOCTYPE html>
<html lang="en-us">
<head>
    <meta name="viewport" content="width=device-width, initial-scale=0.25, maximum-scale=3.0 user-scalable=1">
    <title></title>
    <style>
        body {
            margin: 0 20px 0 0;
            font-family: HelveticaNeue-Light, HelveticaNeue-UltraLight, Helvetica, Consolas, 'Courier New';
        }

        table {
            width: 100%;
            border: 1px outset;
            border-color: #3c4142;
        }

        td {
            font-size: 8px;
        }
    </style>
</head>
<body>
    <img src="data:image/png;base64,YourBase64imagestringhere" style="width:100%" />
</body>
</html>
this.WebView.Source=newhtmlwebviewsource{BaseUrl=URL,Html=Html};
html将在这里定义。
身体{
利润率:0.20px 0.0;
字体系列:HelveticaNeue Light、HelveticaNeue UltraLight、Helvetica、Consoleas,“Courier New”;
}
桌子{
宽度:100%;
边界:1px;
边框颜色:#3c4142;
}
运输署{
字号:8px;
}

使用Nuget的手势先生软件包:

Sten Petrov您的答案可能会有帮助,但我必须只使用Xamarin.Forms。@Amete这是Xamarin.Forms,渲染器是iti的一部分我想要这样的东西,但我无法理解这个src=“data:image/png;base64,YourBase64imagestringhere”,我如何在这里添加静态图像?我的图像路径是“myproject.Images.test.jpg”首先,您需要将图像更改为base 64字符串。请参阅下面的链接,了解如何执行此操作。然后注入base 64结果(这是一个非常大的字符串)并将其放置在占位符中,即我的代码中的Base64ImageStringhere。您必须有一种将数据传递到html的方法。在我的示例中,我使用的是nancy fx的Razor视图。