private Task<Position> WaitForMouseClick(Grid chessboard)
{
var tcs = new TaskCompletionSource<Position>();
tcs = null;
MouseButtonEventHandler mouseLeftButtonDownHandler = null;
mouseLeftButtonDownHandler = (sender, e) =>
{
Point clickPosition = e.GetPosition(chessboard);
int row = (int)(clickPosition.Y / (chessboard.ActualHeight / ROWS));
int col = (int)(clickPosition.X / (chessboard.ActualWidth / COLS));
// 设置任务为已完成,以便继续执行下一步操作
tcs.SetResult(new Position(row, col));
// 解绑事件处理程序,防止重复执行
chessboard.MouseLeftButtonDown -= mouseLeftButtonDownHandler;
};
// 绑定事件处理程序
chessboard.MouseLeftButtonDown += mouseLeftButtonDownHandler;
return tcs.Task;
}
如上,此时程序根本不等鼠标点击就向下执行,在return处报错 tcs==null