我希望对 Room.ToNorth.ImageName
进行数据绑定,而 ToNorth
可能为空。
我正在尝试编写自己的文本冒险游戏,并在每个玩家可以前往的方向旁边放置图像(例如开/关门,通道)。当某个方向没有房间时,我将控件绑定到显示“无出口”图像,这对于四个方向都有出口的起始位置很有效,但是当有一个方向为 null 时,我会收到以下异常:
System.ArgumentNullException: 'Value cannot be null. Parameter name: component'
这在新游戏上不是问题,但我正在制作随机生成的地牢,因此在加载游戏时无法保证。我意识到我可能可以通过创建安全空间来弥补此问题,同时设置绑定,然后将玩家移动到他们需要去的地方,但是否有更好的处理方法呢?
我找到了 这个问题 最接近的答案,但由于绑定的方式不同,我不确定能否应用它。
private GameSession _CurrentGame;
private BindingSource _BindToPlayer = new BindingSource();
private void BtnNewGame_Click(object sender, EventArgs e)
{
_CurrentGame = new GameSession();
_BindToPlayer.DataSource = _CurrentGame.CurrentPlayer;
PicBoxNorth.DataBindings.Add("ImageLocation", _BindToPlayer,
"CurrentRoom.ToNorth.ImageName", true, DataSourceUpdateMode.OnPropertyChanged,
"Images\\Wall.png");
}
ToNorth属性只是从出口数组中获取对象,但如果像上面的链接中建议的那样告诉它返回一个空的出口,那么会存在一个问题,因为该出口将没有任何房间连接,因此很可能无法初始化并抛出异常。更好地解释一下,我的房间设置如下:
public Class Room
{
public int ID { get; set; }
public String Name { get; set; }
public String Description { get; set; }
public String FarDescription { get; set; }
public CoOrds Co_Ords { get; set; }
public Boolean UniqueRoom { get; set; }
public Exit[] ExitArr { get; set; }
public Exit ToNorth => ExitArr[0];
public Exit ToSouth => ExitArr[1];
public Exit ToWest => ExitArr[2];
public Exit ToEast => ExitArr[3];
public Exit ToUp => ExitArr[4];
public Exit ToDown => ExitArr[5];
public Exit ToIn => ExitArr[6];
public Exit ToOut => ExitArr[7];
public Room(int id, String name, String desc, String fardesc,bool unique = false)
{
ID = id;
Name = name;
Description = desc;
FarDescription = fardesc;
Co_Ords = new CoOrds();
ExitArr = new Exit[8];
UniqueRoom = unique;
}
我的出口设置如下所示
public class Exit
{
public String Description {get;}
public Room RoomA { get; set; }
public Room RoomB { get; set; }
public Boolean CanExitA { get; set; } = true;
public Boolean CanExitB { get; set; } = true;
public Room NextRoom
{
get
{
if (RoomA.PlayerHere && CanExitA)
{ return RoomB; }
else if (RoomB.PlayerHere && CanExitB)
{ return RoomA; }
else
return null;
}
}
public String ImageName
{
get
{
string imagePath = "Images\\";
if (DoorHere != null)
{
return imagePath + DoorHere.ImageName;
}
else
return imagePath + "Pathway.png";
}
}
public Door DoorHere { get; set; }
public Exit(Room roomA, int Adir, Room roomB, int Bdir, Door door = null)
{
RoomA = roomA;
RoomA.ExitArr[Adir] = this;
RoomB = roomB;
RoomB.ExitArr[Bdir] = this;
DoorHere = door;
}
门只是一个未命名的对象,具有一些布尔类型的属性,例如IsOpen、IsLocked等,如果您想测试,则不需要它,但出口是匿名创建的,并将自己添加到连接房间中的Exit数组中,因此创建空出口会失败。
任何建议都将不胜感激。