如何从方法内部获得调用该方法的对象

本文关键字:方法 调用 对象 内部 | 更新日期: 2023-09-27 18:04:30

我的设置是这样的;在我为Windows Phone 8编写的c#程序中,我有多个Ellipse元素,当鼠标进入其中一个元素时,它们都调用相同的方法Checkpoint。问题是,由于我将在最近输入的椭圆和之前输入的椭圆之间绘制一条线,因此我需要知道任何给定调用来自哪个椭圆。如果有帮助的话,代码如下:

Point old;
private void CheckPoint(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (old.Equals(null))
            {
                old.Equals(this.);
            } 
            else
            {
                System.Windows.Shapes.Line connectline = new System.Windows.Shapes.Line();
                connectline.X1 = old.Margin.Left;
                connectline.Y1 = old.Margin.Top;
                connectline.X2 = this. ;
                connectline.Y2 = this.
            }
        }

可以看到,这段代码是不完整的;Old应该被设置为运行完代码块后按下的椭圆。"this."是不完整的,并且将被调用该方法的椭圆的边缘属性所取代。感谢所有!

如何从方法内部获得调用该方法的对象

您可以通过

来识别哪个是Selected Ellipse
    private void CheckPoint(object sender, System.Windows.Input.MouseEventArgs e)
    {
      var selectedEllipse = sender as Ellipse;
      if(selectedEllipse!=null)
        {
          //Your code here
        }
    }