有没有办法让控件允许我捏和缩放使用只有 Xamarin.Forms控件。Xamarin.forms捏和缩放
我想在任何来自xamarin.forms(WebView或图像或任何其他)的控件中显示图像,并且能够从应用程序中缩放。
有没有办法让控件允许我捏和缩放使用只有 Xamarin.Forms控件。Xamarin.forms捏和缩放
我想在任何来自xamarin.forms(WebView或图像或任何其他)的控件中显示图像,并且能够从应用程序中缩放。
我结束了使用元视口进行缩放如下。 这可能不是每个人的解决方案,但它为我工作。
将图像十六进制-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>
截至本帖发表时,没有办法用纯内置的Forms控件捏或放大。有一种方法可以实现这一点,但您必须为此实现本地渲染器。
我在创建一个继承自Xamarin.Forms.ContentView - PanGestureContainer的应用程序中创建了一个类,该类具有诸如触点数min/max和要侦听的事件等属性。
在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());
}
}
嗨斯登彼得罗夫。你可以请发一个伪代码吗? – 2014-09-02 15:27:37
在这里,祝你好运:) – 2014-09-02 17:35:11
Sten Petrov你的答案可能会有所帮助,但我只能使用Xamarin.Forms。 – 2015-01-19 16:24:47
使用Mr.Gestures包从的NuGet:https://www.nuget.org/packages/MR.Gestures/
我想要这样的东西,但我不能理解这个src =“data:image/png; base64,YourBase64imagestringhere”,我如何添加我的静态图像这里 ??我的图像路径是“myproject.Images.test.jpg” – 2015-11-06 11:10:20
首先,您需要将图像更改为base 64字符串请参阅下面的链接以了解如何执行此操作http://stackoverflow.com/questions/21325661/convert-image-路径到的base64字符串。然后你注入基础64结果(这是非常大的字符串),并将其放置在我的代码中YourBase64imagestring的占位符中。你必须有一种将数据传递给html的方法。在我的情况下,我使用nancy fx的Razor视图。 – 2015-11-09 15:16:35