传递参数以覆盖绘制方法时可变丢失数据

时间:2015-01-06 16:33:27

标签: c# ios xamarin drawing pass-data

视图控制器的类:

namespace DrawOverride
    {
        public partial class DrawOverrideViewController : UIViewController
        {
            float W, H;
            CGPoint initialPoint;
            Drawer dr;

            public DrawOverrideViewController (IntPtr handle) : base (handle)
            {
            }

            public DrawOverrideViewController ()
            {

            }

            public override void DidReceiveMemoryWarning ()
            {
                // Releases the view if it doesn't have a superview.
                base.DidReceiveMemoryWarning ();

                // Release any cached data, images, etc that aren't in use.
            }

            #region View lifecycle

            public override void ViewDidLoad ()
            {
                base.ViewDidLoad ();
                initialPoint = new CGPoint(5, 5);
                dr = new Drawer (initialPoint);
                **Console.WriteLine("1 - "+initialPoint);**
                // Perform any additional setup after loading the view, typically from a nib.
            }

            public override void ViewWillAppear (bool animated)
            {
                base.ViewWillAppear (animated);
            }

            public override void ViewDidAppear (bool animated)
            {
                base.ViewDidAppear (animated);
            }

            public override void ViewWillDisappear (bool animated)
            {
                base.ViewWillDisappear (animated);
            }

            public override void ViewDidDisappear (bool animated)
            {
                base.ViewDidDisappear (animated);
            }

            #endregion

            public override void TouchesBegan (NSSet touches, UIEvent evt)
            {
                base.TouchesBegan (touches, evt);
                UITouch touch = touches.AnyObject as UITouch;
        }
    }

第二节课:

namespace DrawOverride
{
    partial class Drawer : UIView
    {
        CGPath pathed;

        CGPoint initialPoint;
        CGPoint latestPoint;

        public Drawer (IntPtr handle) : base (handle)
        {
        }

        public Drawer (CGPoint a)
        {
            initialPoint = a;
            **Console.WriteLine ("2 - "+a);**
        }

        public override void TouchesBegan (NSSet touches, UIEvent evt)
        {
            base.TouchesBegan (touches, evt);
        }

        public override void TouchesMoved (NSSet touches, UIEvent evt)
        {
            base.TouchesMoved (touches, evt);
            UITouch touch = touches.AnyObject as UITouch;
            latestPoint = touch.LocationInView (this);
            SetNeedsDisplay ();
        }

        public override void TouchesEnded (NSSet touches, UIEvent evt)
        {
            base.TouchesMoved (touches, evt);
        }

        public override void Draw (CGRect rect)
        {
            base.Draw (rect);
            pathed = new CGPath ();

            using (CGContext g = UIGraphics.GetCurrentContext ()) {
                g.SetLineWidth (8);
                UIColor.Blue.SetFill ();
                UIColor.White.SetStroke ();
                **Console.WriteLine("3 - "+initialPoint);**
                if (pathed.IsEmpty) {
                    pathed.AddLines (new CGPoint []{ initialPoint, latestPoint });
                } else {
                    pathed.AddLineToPoint (latestPoint);
                }

                //add geometry to graphics context and draw it
                g.AddPath (pathed);
                g.DrawPath (CGPathDrawingMode.FillStroke);
            }
        }
    }
}

在“Console.WriteLine”1和2中,变量仍然有一个值,(5,5),就像我做的那样。在3,在重写绘制方法内,值的打印为{0,0}。我已经尝试过了:

Drawer dr = new Drawer(); dr.initialPoint = initialPoint;

但它也不起作用..

0 个答案:

没有答案