Xamarin形式:本机控件onDraw永远不会被调用

时间:2016-08-09 23:46:10

标签: android xamarin custom-controls ondraw renderer

我有构建原生控件的xamarin形式。我已经扩展了ViewRenderer,并且本机控件中的touch事件可以检测鼠标事件。另一方面,我重写onDraw但它永远不会被调用,即使我调用invalidate和setWillNotDraw为false。

public class CanvasViewRenderer : ViewRenderer<CanvasView, ImageView> 
{
    private ImageView _ImageView;

    protected override void OnElementChanged(ElementChangedEventArgs<CanvasView> e)
    {
        base.OnElementChanged(e);

        if (Control == null)
        {
            _ImageView = new ImageView(Context);
            page = new Page();
            var directory = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads);
            var imagePath = directory + "/A.png";
            _ImageView.SetImageURI(Android.Net.Uri.Parse("file://" + imagePath));
            _ImageView.SetWillNotDraw(false);
            SetNativeControl(_ImageView);
        }
        if (e.OldElement != null)
        { 
            Control.Touch -= CanvasTouchEvent;
        }
        if (e.NewElement != null)   
        { 
            Control.Touch += CanvasTouchEvent;
        }
    }

    protected override void OnDraw(Canvas canvas)
    {
        base.OnDraw(canvas);

        var strokes = page.GetStrokes();

        if (strokes != null && strokes.Count() > 0)
        {
            for (int i = 0; i < strokes.Count(); i++)
            {
                var stroke = strokes.ElementAt(i);

                if (stroke.GetPointerId() != -1)
                { 
                    if (stroke.GetPath() != null)
                        canvas.DrawPath(stroke.GetPath(), new Paint());
                }
            }
        }
    }

0 个答案:

没有答案