'System.Windows.Rect' does not contain a definition for 'Intersects' for C#

送分小仙女□ 提交于 2019-12-11 08:54:43

问题


I am doing a collision on Windows Phone App for Windows Phone 8 using Microsoft Visual Studio 2012. I used Rect to make a rectangle border for the ball. I received this error. 'System.Windows.Rect' does not contain a definition for 'Intersects' and no extension method 'Intersects' accepting a first argument of type 'System.Windows.Rect' could be found (are you missing a using directive or an assembly reference?) The codes are as below.

var greenBallPositionX1 = Canvas.GetLeft(this.greenBall1);
var greenBallPositionY1 = Canvas.GetTop(this.greenBall1);

Rect r1 = new Rect(greenBallPositionX1, greenBallPositionY1, greenBall1.ActualWidth, greenBall1.ActualHeight);

var blueBallPositionX1 = Canvas.GetLeft(this.blueBall1);
var blueBallPositionY1 = Canvas.GetTop(this.blueBall1);

Rect r2 = new Rect(blueBallPositionX1, blueBallPositionY1, blueBall1.ActualWidth, blueBall1.ActualHeight);

if (r1.Intersects(r2))
        {
            MessageBox.Show("Collision Detected");
        }

回答1:


your own intersects can be written as

public bool Intersects(Rect r1,Rect r2)
{
  r1.Intersect(r2);

  if(r1.IsEmpty)
  {
    return false;
  }
  else 
  {
    return true;
  }
}

then you can use

if(Intersects(r1,r2))
{
  MessageBox.Show("Collison Detected");
}



回答2:


The syntax is:

Rect.Intersect(Rect)

Lose the "s"!

http://msdn.microsoft.com/en-us/library/ms558125.aspx



来源:https://stackoverflow.com/questions/17443624/system-windows-rect-does-not-contain-a-definition-for-intersects-for-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!