最近新学C#,按照Visual c# 2010从入门到精通里的实例写程序,只写了一部分就报错了,看了几次和书上写的一样,请高手指点,谢谢。代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows;
using System.Windows.Shapes;namespace Drawing
{
class Square:IDraw, IColor
{
private int sideLength;
private int locX = 0, locY = 0;
private Rectangle rect = null;
public Square(int sideLength)
{
this.sideLength = sideLength;
} void IDraw.SetLocation(int xCoord, int yCoord)
{
this.locX = xCoord;
this.locY = yCoord;
} void IDraw.Draw(System.Windows.Controls.Canvas canvas)
{
if (this.rect != null)
{
canvas.Children.Remove(this.rect);
}
else
{
this.rect = new Rectangle();
}
this.rect.Height = this.sideLength;
this.rect.Width = this.sideLength;
canvas.SetTop(this.rect, this.locY);
canvas.SetLeft(this.rect, this.locX);
canvas.Children.Add(rect);
}
}
}错误提示:
错误 1 无法使用实例引用来访问成员“System.Windows.Controls.Canvas.SetTop(System.Windows.UIElement, double)”;请改用类型名来限定它
错误 2 无法使用实例引用来访问成员“System.Windows.Controls.Canvas.SetLeft(System.Windows.UIElement, double)”;请改用类型名来限定它
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows;
using System.Windows.Shapes;namespace Drawing
{
class Square:IDraw, IColor
{
private int sideLength;
private int locX = 0, locY = 0;
private Rectangle rect = null;
public Square(int sideLength)
{
this.sideLength = sideLength;
} void IDraw.SetLocation(int xCoord, int yCoord)
{
this.locX = xCoord;
this.locY = yCoord;
} void IDraw.Draw(System.Windows.Controls.Canvas canvas)
{
if (this.rect != null)
{
canvas.Children.Remove(this.rect);
}
else
{
this.rect = new Rectangle();
}
this.rect.Height = this.sideLength;
this.rect.Width = this.sideLength;
canvas.SetTop(this.rect, this.locY);
canvas.SetLeft(this.rect, this.locX);
canvas.Children.Add(rect);
}
}
}错误提示:
错误 1 无法使用实例引用来访问成员“System.Windows.Controls.Canvas.SetTop(System.Windows.UIElement, double)”;请改用类型名来限定它
错误 2 无法使用实例引用来访问成员“System.Windows.Controls.Canvas.SetLeft(System.Windows.UIElement, double)”;请改用类型名来限定它
public static void SetTop(
UIElement element,
double length
)
静态方法不属于特定实例
这是个静态的方法
System.Windows.Controls.Canvas.SetLeft(this.rect, this.locX);
System.Windows.Controls.Canvas.Children.Add(rect);