我想执行数据绑定,Room.ToNorth.ImageName而while ToNorth可能为null.
我正在尝试编写自己的文本冒险,并且在玩家可以前往的每个方向旁边都有图像(即打开/关闭的门,通道).当在给定方向上没有房间时,我有控件绑定显示"无退出"图像,这很有效,所以玩家起始位置在所有4个方向都有一个出口,但是当一个为空时,我得到了以下例外
System.ArgumentNullException:'值不能为null.参数名称: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");
}
Run Code Online (Sandbox Code Playgroud)
ToNorth属性只是从一个出口数组中获取对象,但如果它为null(如上面的链接中所示),则告诉它返回一个空出口将会出现问题,因为出口没有任何连接的房间和因此无法初始化可能会在此过程中抛出异常.为了更好地解释,我的房间设置如下
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; …Run Code Online (Sandbox Code Playgroud)