2014-08-29 131 views
4

有沒有辦法讓控件允許我捏和縮放使用只有 Xamarin.Forms控件。Xamarin.forms捏和縮放

我想在任何來自xamarin.forms(WebView或圖像或任何其他)的控件中顯示圖像,並且能夠從應用程序中縮放。

回答

1

我結束了使用元視口進行縮放如下。 這可能不是每個人的解決方案,但它爲我工作。

將圖像十六進制-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> 
+0

我想要這樣的東西,但我不能理解這個src =「data:image/png; base64,YourBase64imagestringhere」,我如何添加我的靜態圖像這裏 ??我的圖像路徑是「myproject.Images.test.jpg」 – 2015-11-06 11:10:20

+0

首先,您需要將圖像更改爲base 64字符串請參閱下面的鏈接以瞭解如何執行此操作http://stackoverflow.com/questions/21325661/convert-image-路徑到的base64字符串。然後你注入基礎64結果(這是非常大的字符串),並將其放置在我的代碼中YourBase64imagestring的佔位符中。你必須有一種將數據傳遞給html的方法。在我的情況下,我使用nancy fx的Razor視圖。 – 2015-11-09 15:16:35

1

截至本帖發表時,沒有辦法用純內置的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()); 
    } 
} 
+0

嗨斯登彼得羅夫。你可以請發一個僞代碼嗎? – 2014-09-02 15:27:37

+0

在這裏,祝你好運:) – 2014-09-02 17:35:11

+0

Sten Petrov你的答案可能會有所幫助,但我只能使用Xamarin.Forms。 – 2015-01-19 16:24:47