如何在c#WPF中从多个类调用接口方法



我正在尝试在c#WPF中创建一个chessgame。我有多个类,每种棋子都有一个,它们都实现了相同的接口。所有片段对象都存储在类型为object[,]的2d数组中(其中有一种感觉,这不是正确的方式(。我想通过调用Board[x,y].ImgURI循环遍历这个数组来绘制每个对应的图像,但我得到了:

CS1061"object"不包含"ImgURI"的定义,并且找不到接受"object"类型的第一个参数的可访问扩展方法"ImgURI">

class Rook : Igamepiece{...}
class King : Igamepiece{...}
class Queen : Igamepiece{...}
interface Igamepiece{
public string ImgURI { get; set; }     //property that holds the image Uri
}
class Main{
public object[,] Board = new object[8, 8];         //array containing objects of different types
for (int y = 0; y < Board.GetLength(0); y++)
{
for (int x = 0; x < Board.GetLength(1); x++)
{
GameArea.Children.Add(new Image
{
Source = new BitmapImage(new Uri(Board[x, y].ImgURI, UriKind.Relative)),
Width = SquareSize,
Height = SquareSize,
Margin = new Thickness(nextX, nextY, 0, 0)
});
nextX += SquareSize;
}
nextX = 0;
nextY += SquareSize;
}
}           

只需更改:

public object[,] Board = new object[8, 8]; 

至:

public Igamepiece[,] Board = new Igamepiece[8, 8]; 

相关内容

最新更新