有没有办法让控件允许我使用 ONLY Xamarin.Forms控件进行缩放和缩放。
我希望在xamarin.forms(WebView或Image或任何其他)的任何控件中显示图像,并能够从应用程序进行缩放。
答案 0 :(得分:1)
截至本文发布之时,无法使用纯内置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());
}
}
答案 1 :(得分:1)
我最终使用元视口进行缩放,如下所示。 这可能不是每个人的解决方案,但它对我有用。
将图像hex -64放在下面的image标签内,然后将整个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>
答案 2 :(得分:0)
使用Nuget的Mr.Gestures包:https://www.nuget.org/packages/MR.Gestures/