“System.Windows.Rect"不包含 C# 的“Intersects"定义 [英] 'System.Windows.Rect' does not contain a definition for 'Intersects' for C#

查看:29
本文介绍了“System.Windows.Rect"不包含 C# 的“Intersects"定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Microsoft Visual Studio 2012 在适用于 Windows Phone 8 的 Windows Phone 应用程序上进行碰撞.我使用 Rect 为球制作矩形边框.我收到了这个错误.System.Windows.Rect"不包含Intersects"的定义,并且找不到接受System.Windows.Rect"类型的第一个参数的扩展方法Intersects"(您是否缺少 using 指令或程序集引用?) 代码如下.

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");
        }

推荐答案

你自己的相交可以写成

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

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

然后你可以使用

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

这篇关于“System.Windows.Rect"不包含 C# 的“Intersects"定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆