Xamarin.forms 捏合和缩放

4

有没有一种方法可以使用Xamarin.Forms控件来实现捏合缩放的控制。

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

3个回答

1
我最终使用了meta viewport进行缩放,如下所示。 这可能不是每个人的解决方案,但对我有效。
将图像hex-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>

我想要类似这样的东西,但我不理解这个src="data:image/png;base64,YourBase64imagestringhere",我该如何在这里添加我的静态图片?我的图片路径是“myproject.Images.test.jpg”。 - G.Mich
首先,您需要将图像更改为base64字符串,请参阅下面的链接以了解如何执行此操作https://dev59.com/5GEi5IYBdhLWcg3wYLeT。然后,您将注入base64结果(这是非常大的字符串),并将其放置在占位符中,该占位符是我的代码中的YourBase64imagestringhere。您必须有一种将数据传递到html的方法。在我的情况下,我正在使用来自nancy fx的Razor视图。 - Helen Araya

1
截至发布时,纯内置Forms控件没有捏/缩放的方式。有一种方法可以实现这个功能,但需要实现本地渲染器。
我在编写的应用程序中通过创建一个从Xamarin.Forms.ContentView继承的类 - PanGestureContainer 来实现了这一点,该类具有诸如触摸点数量上下限和事件监听等属性。
在iOS项目中,我为我的视图制作了一个自定义渲染器,渲染器从视图中获取属性并连接触摸事件监听器。
此外,我制作了一种可附加的属性(又称行为),可以应用于其他视图,并在应用时将视图从其父级中取出,包裹在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());
    }
}

Sten Petrov,你的回答可能有帮助,但我必须只使用Xamarin.Forms。 - Helen Araya
@Amete 这是 Xamarin.Forms,渲染器是其中的一部分。 - Sten Petrov

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接