2014-04-26 24 views
2

,我打算給小部件綁定到視圖模型性質,但我發現一個異常Mvvmcross綁定

MvxBind:Warning: 14.76 Failed to create target binding for binding Signature for Order.ClientSignature 
[0:] MvxBind:Warning: 14.76 Failed to create target binding for binding Signature for Order.ClientSignature 
04-26 21:02:15.380 I/mono-stdout(32490): MvxBind:Warning: 14.76 Failed to create target binding for binding Signature for Order.ClientSignature 

小部件是Al taiar

禮貌的axml是

<SignatureWidget 
     android:layout_width="match_parent" 
     android:layout_height="100dp" 
     android:id="@+id/signatureWidget1" 
     android:layout_marginRight="5dp" 
     android:layout_marginLeft="5dp" 
     android:layout_marginBottom="5dp" 
     local:MvxBind="Signature Order.ClientSignature" /> 

的該視圖的代碼是

using Android.Content; 
using Android.Graphics; 
using Android.Util; 
using Android.Views; 
using Core.Models; 
using System; 

public class SignatureWidget 
    : View 
{ 
    #region Implementation 

    private Bitmap _bitmap; 
    private Canvas _canvas; 
    private readonly Path _path; 
    private readonly Paint _bitmapPaint; 
    private readonly Paint _paint; 

    private float _mX, _mY; 
    private const float TouchTolerance = 4; 

    #endregion 

    public Signature Signature; 

    public event EventHandler SignatureChanged; 

    public SignatureWidget(Context context, IAttributeSet attrs) 
     : base(context, attrs) 
    { 
     Signature = new Signature(); 

     _path = new Path(); 
     _bitmapPaint = new Paint(PaintFlags.Dither); 
     _paint = new Paint 
     { 
      AntiAlias = true, 
      Dither = true, 
      Color = Color.Argb(250, 00, 0, 0) 
     }; 
     _paint.SetStyle(Paint.Style.Stroke); 
     _paint.StrokeJoin = Paint.Join.Round; 
     _paint.StrokeCap = Paint.Cap.Round; 
     _paint.StrokeWidth = 5; 
    } 

    protected override void OnSizeChanged(int w, int h, int oldw, int oldh) 
    { 
     base.OnSizeChanged(w, h, oldw, oldh); 
     _bitmap = Bitmap.CreateBitmap(w, (h > 0 ? h : ((View)this.Parent).Height), Bitmap.Config.Argb8888); 
     _canvas = new Canvas(_bitmap); 
    } 

    protected override void OnDraw(Canvas canvas) 
    { 
     canvas.DrawColor(Color.White); 
     canvas.DrawBitmap(_bitmap, 0, 0, _bitmapPaint); 
     canvas.DrawPath(_path, _paint); 
    } 

    private void TouchStart(float x, float y) 
    { 
     _path.Reset(); 
     _path.MoveTo(x, y); 
     _mX = x; 
     _mY = y; 
     Signature.AddPoint(SignatureState.Start, (int)x, (int)y); 
    } 

    private void TouchMove(float x, float y) 
    { 
     float dx = Math.Abs(x - _mX); 
     float dy = Math.Abs(y - _mY); 

     if (dx >= TouchTolerance || dy >= TouchTolerance) 
     { 
      _path.QuadTo(_mX, _mY, (x + _mX)/2, (y + _mY)/2); 
      Signature.AddPoint(SignatureState.Move, (int)x, (int)y); 
      _mX = x; 
      _mY = y; 
     } 
    } 

    private void TouchUp() 
    { 
     if (!_path.IsEmpty) 
     { 
      _path.LineTo(_mX, _mY); 
      _canvas.DrawPath(_path, _paint); 
     } 
     else 
     { 
      _canvas.DrawPoint(_mX, _mY, _paint); 
     } 
     Signature.AddPoint(SignatureState.End, (int)_mX, (int)_mY); 

     _path.Reset(); 
    } 

    public override bool OnTouchEvent(MotionEvent e) 
    { 
     var x = e.GetX(); 
     var y = e.GetY(); 

     switch (e.Action) 
     { 
      case MotionEventActions.Down: 
       TouchStart(x, y); 
       Invalidate(); 
       break; 
      case MotionEventActions.Move: 
       TouchMove(x, y); 
       Invalidate(); 
       break; 
      case MotionEventActions.Up: 
       TouchUp(); 
       Invalidate(); 
       break; 
     } 

     RaiseSignatureChangedEvent(); 

     return true; 
    } 

    public void ClearCanvas() 
    { 
     _canvas.DrawColor(Color.White); 
     Invalidate(); 
    } 

    public Bitmap CanvasBitmap() 
    { 
     return _bitmap; 
    } 

    public void Clear() 
    { 
     ClearCanvas(); 
     Signature.Clear(); 

     RaiseSignatureChangedEvent(); 
    } 

    private void RaiseSignatureChangedEvent() 
    { 
     var handler = SignatureChanged; 
     if (handler != null) 
      handler(this, EventArgs.Empty); 
    } 
} 

而對於模型的代碼是

public class Signature 
{ 
    private List<Point> _currentPath; 
    private readonly List<List<Point>> _paths; 

    public event EventHandler PointAdded; 

    public Signature() 
    { 
     _currentPath = new List<Point>(); 
     _paths = new List<List<Point>>(); 
    } 

    public IReadOnlyList<IReadOnlyList<Point>> Paths 
    { 
     get { return _paths; } 
    } 

    public Point LastPoint() 
    { 
     if (_currentPath != null && _currentPath.Count > 0) 
     { 
      return _currentPath.Last(); 
     } 

     return new Point(0, 0); 
    } 

    public void Clear() 
    { 
     _paths.Clear(); 
     _currentPath.Clear(); 
    } 

    public void AddPoint(SignatureState state, int x, int y) 
    { 
     if (state == SignatureState.Start) 
     { 
      _currentPath = new List<Point>(); 
     } 
     if (x != 0 && y != 0) 
     { 
      _currentPath.Add(new Point(x, y)); 
     } 
     if (state == SignatureState.End) 
     { 
      if (_currentPath != null) 
      { 
       _paths.Add(_currentPath); 
      } 
     } 

     RaisePointAddedEvent(); 
    } 

    public int Length 
    { 
     get { return _paths.Count; } 
    } 

    protected void RaisePointAddedEvent() 
    { 
     if (PointAdded != null) 
      PointAdded(this, EventArgs.Empty); 
    } 
} 

我需要雙向的這個小工具的結合。任何人都在意幫助嗎?

我還需要在視圖上添加一個「清除」文本作爲覆蓋。點擊這個文本將觸發一個命令來清除小部件。任何線索如何做到這一點?

P.S: 我按照信息post,我仍然無法使它工作。我已經添加了以下內容。

public class SignatureWidgetSignatureTargetBinding 
    : MvxPropertyInfoTargetBinding<SignatureWidget> 
{ 
    public SignatureWidgetSignatureTargetBinding(object target, PropertyInfo targetPropertyInfo) 
     : base(target, targetPropertyInfo) 
    { 
     View.SignatureChanged += OnSignatureChanged; 
    } 

    public override MvxBindingMode DefaultMode 
    { 
     get { return MvxBindingMode.TwoWay; } 
    } 

    private void OnSignatureChanged(object sender, EventArgs eventArgs) 
    { 
     FireValueChanged(View.Signature); 
    } 

    protected override void Dispose(bool isDisposing) 
    { 
     base.Dispose(isDisposing); 
     if (isDisposing) 
     { 
      View.SignatureChanged -= OnSignatureChanged; 
     } 
    } 
} 

並註冊使用

registry.RegisterFactory(new MvxSimplePropertyInfoTargetBindingFactory(typeof(SignatureWidgetSignatureTargetBinding), typeof(SignatureWidget), "Signature")); 

回答

0

,如果你使用的格式模型,可以MvvmCross將自動綁定視圖物業

public foo Bar { 
    get { /* ... your code ... */ } 
    set { /* ... your code ... */ } 
    } 

    public event EventHandler BarChanged; 

在此基礎上,我認爲你的問題是,您正在嘗試使用字段 - public Signature Signature; - 請嘗試使用屬性。

我認爲你正在尋找的結合模式也是不同尋常的OneWayToSource而不是TwoWay

+0

我敢肯定,我在我讀過的職位碰到過這樣的提醒,我想我一定是勞累過度不要注意到這個錯誤。謝謝一堆。 – Telemat

+0

酒吧會是一個副本還是應該指向VM屬性? – Telemat