简体   繁体   English

如何从方法中获取调用方法的对象

[英]How to get the object which called a method from within the method

So my setup is as such; 所以我的设置是这样的; I have, in my C# program for Windows Phone 8, multiple Ellipse elements, all of which call the same method, Checkpoint, when the mouse enters one. 在我的Windows Phone 8的C#程序中,我有多个Ellipse元素,当鼠标进入一个时,所有这些元素都调用相同的方法Checkpoint。 The problem is that since I will be drawing a line between the more recently entered ellipse and the previously entered ellipse, I need to know which ellipse any given call came from. 问题在于,由于我将在最近输入的椭圆和先前输入的椭圆之间画一条线,我需要知道任何给定调用来自哪个椭圆。 If it helps, the code is below: 如果有帮助,代码如下:

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.
            }
        }

As you can see, this code is incomplete; 如您所见,此代码不完整; old is supposed to be set to whichever ellipse is pressed after it runs through the code block. old应该被设置为在遍历代码块之后按下的椭圆。 The "this." “这个。” are incomplete, and are to be substituted with the margin properties from the ellipse that called the method. 是不完整的,并且将被调用方法的椭圆的边距属性替换。 Thanks all! 谢谢大家!

You can identify which is the Selected Ellipse by 您可以识别哪个是Selected Ellipse

    private void CheckPoint(object sender, System.Windows.Input.MouseEventArgs e)
    {
      var selectedEllipse = sender as Ellipse;

      if(selectedEllipse!=null)
        {
          //Your code here
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM