如何“;“连接”;对象,使它们相互更新

本文关键字:更新 连接 对象 如何 | 更新日期: 2023-09-27 18:22:30

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

我有一个类,它包含一些线、点和"标记"。

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

Marker类具有拖放实现,可以移动椭圆并更改Marker.coordinates属性。这很管用。

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

我想,当我创建Marker对象并将"SomeShape.lineEnds[0]"传递给Marker构造函数时,Marker类上的update也会更新我的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
}

如何“;“连接”;对象,使它们相互更新

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

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

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

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

我认为,因为结构不是"引用类型"。

我创建了自己的"MyPoint"类,它具有X和Y属性,并且成功了。