0

我有复杂的问题(也许有简单的答案)。

我有课,其中包含一些线条、点和“标记”。

Marker是一个包含Ellipse其中心坐标 ( Point) 的类。

Marker类具有拖放实现,可移动椭圆并更改Marker.coordinates属性。这样可行。

但是,我想使用Marker类中的拖放来移动 SomeShape 对象中的点(Marker对象是 的一部分SomeShape)。

我想,当我创建Marker对象并将 'SomeShape.lineEnds[0]' 传递给Marker构造函数时 - Marker 类的更新也会更新我的SomeShape.lineEnds[0],但它不起作用。

我该如何解决这个问题?通过以某种方式使用一些参考?

我希望我足够清楚地描述了我的问题。

代码:

class SomeShape
{
    // this object is set of lines and "Markers" (class below)

    private List<Marker> markers;
    private List<Point> lineEnds;
    private List<Line> lines;

    // my object can redraw itself on canvas
    public RedrawMe()
    {
        // it removes own lines from canvas and it puts new lines 
        // I call this function after I add points to lineEnds collection etc.
        // or when I change coordinates on one of lineEnds (list of points)

    }

    public void AddPoint(Point p)
    {
        this.lineEnds.Add(p); // adding point to line ends
        this.markers.Add(new Marker(p, this, c)); // adding same point to new marker

        RedrawMe();
    }

}

有问题的部分:

class Marker
{
    public Canvas canvas;

    private Ellipse e;
    private Point coordinates; // ellipse center coordinates
    private Object parent; // I store SomeShape object here to call RedrawMe method on it


    public Marker(Point p, Object par, Canvas c)
    {
        this.coordinates = p;
        this.canvas = c;
        this.parent = par;

        e = MyClassFactory.EllipseForMarker();

        e.MouseDown += new System.Windows.Input.MouseButtonEventHandler(e_MouseDown);
        e.MouseMove += new System.Windows.Input.MouseEventHandler(e_MouseMove);
        e.MouseUp += new System.Windows.Input.MouseButtonEventHandler(e_MouseUp);

        c.Children.Add(e);

        e.Margin = new Thickness(p.X - (e.Width/2), p.Y -  (e.Height/2), 0, 0);
    }

    public void MoveIt(Point nc) // nc - new coordinates
    {
        this.e.Margin = new Thickness(nc.X - (e.Width / 2), nc.Y - (e.Height / 2), 0, 0);
        this.coordinates.X = nc.X;
        this.coordinates.Y = nc.Y;

        if (this.parent is SomeShape) ((SomeShape)parent).RedrawMe();
    }

    #region DragDrop  // just drag drop implementation, skip this


            private bool is_dragged = false;

        void e_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e){
            e.Handled = true;
            is_dragged = false;
            this.e.ReleaseMouseCapture();
        }
        void e_MouseMove(object sender, System.Windows.Input.MouseEventArgs e) {
            if (is_dragged)
            {
                this.MoveIt(e.GetPosition(canvas));
            }
        }
        void e_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e) {
            is_dragged = true;
            this.e.CaptureMouse();
        }

    #endregion // DragDrop
}
4

2 回答 2

0

至于您的问题,您想“通知”父级,尽管有很多方法可以解决这个问题,经典的 C# 方式是创建一个事件并在需要时触发它。

另一方面,您有来自构造函数的父级。我认为您应该考虑接收这样的接口,该接口IDrawable将包含您要从父级调用的任何方法,并通过该接口通知父对象它可以重绘。

在拖放中,我没有看到任何通知父母的代码,因此无法发表评论。但为什么不直接激活同一个呼叫((SomeShape)parent).RedrawMe()呢?

于 2013-07-15T05:40:20.170 回答
0

Point 是一个结构,而不是类,它不会像这样工作。

我假设,因为结构不是“引用类型”。

我创建了自己的具有 X 和 Y 属性的“MyPoint”类,并且它有效。

于 2013-07-15T06:32:26.963 回答