如何通知 UI 在 API 中完成的操作?
How to notify UI about action done in API?
我正在写一个简单的游戏,比如井字游戏(只有我的更大)。游戏玩法很简单:轮到我,检查是否赢了,轮到机器人,检查是否赢了。我有简单的 UI
和 API
使用 Domain Entites
(现在不重要)。因此,当用户移动时,API 将更新棋盘,知道下一步是机器人的移动,所以会这样做并且...必须通知 UI。这是我的问题。
我的问题是:
如何通知 UI 机器人的移动? 我的意思是,保持简单但坚持最佳编程实践。
我的第一个想法是在游戏中创建一个事件API class。这是个好主意吗?今天将介绍所有新内容,C# 6 等。我不确定:/ 现在 UI 是 WinForms
,但我想在其他 [=37] 中使用这个 API =]s,像 WPF 甚至移动。这是我的 UI:
的简化代码
编辑: 现在我说的是单人游戏。 UI 和 API 都是客户。下一步将通过中央服务器进行多人游戏,但现在是单人游戏。
public partial class Form1 : Form
{
private GameAPI api;
public Form1()
{
InitializeComponent();
api = new GameAPI();
}
private void boardClick(object sender, EventArgs e)
{
Field field = GetClickedField(e);
MoveResult result = api.MakeMove(clickedColumn);
if (result != null && result.Row >= 0)
{
MessageBox.Show(result.Row + "," + clickedColumn);
if (result.IsConnected)
{
MessageBox.Show("Success!");
}
}
}
}
和API:
public class GameAPI
{
public IGame CurrentGame { get; set; }
public void CreateGame(GameType type)
{
CurrentGame = new SinglePlayerGame();
}
public Result Move(int column)
{
if (CurrentGame == null) return null;
Player player = CurrentGame.GetNextPlayer();
if (player.Type == PlayerType.Human) return CurrentGame.Move(column, player.Id);
}
public Result MoveBot()
{
// Simulate Bot's move...
}
}
My first thought was to create an event in GameAPI class. Is that good idea?
是的,为什么不呢?让我们以现代 UI 框架数据绑定为例。使数据绑定工作的关键点是在修改对象的某些 属性 值时提供 属性 更改通知(读取 - 事件)。通常这是通过 IPropertyNotifyChanged
接口实现的,这只是声明支持 PropertyChanged
event 的一种多态方式。这样,如果您通过代码设置对象 属性,UI 会自动更新。希望您看到与您的案例的相似之处 - API 做了一些事情并引发了一个事件,UI(作为较早的一点附加到该事件的处理程序)接收事件并相应地更新。
我正在写一个简单的游戏,比如井字游戏(只有我的更大)。游戏玩法很简单:轮到我,检查是否赢了,轮到机器人,检查是否赢了。我有简单的 UI
和 API
使用 Domain Entites
(现在不重要)。因此,当用户移动时,API 将更新棋盘,知道下一步是机器人的移动,所以会这样做并且...必须通知 UI。这是我的问题。
我的问题是: 如何通知 UI 机器人的移动? 我的意思是,保持简单但坚持最佳编程实践。
我的第一个想法是在游戏中创建一个事件API class。这是个好主意吗?今天将介绍所有新内容,C# 6 等。我不确定:/ 现在 UI 是 WinForms
,但我想在其他 [=37] 中使用这个 API =]s,像 WPF 甚至移动。这是我的 UI:
编辑: 现在我说的是单人游戏。 UI 和 API 都是客户。下一步将通过中央服务器进行多人游戏,但现在是单人游戏。
public partial class Form1 : Form
{
private GameAPI api;
public Form1()
{
InitializeComponent();
api = new GameAPI();
}
private void boardClick(object sender, EventArgs e)
{
Field field = GetClickedField(e);
MoveResult result = api.MakeMove(clickedColumn);
if (result != null && result.Row >= 0)
{
MessageBox.Show(result.Row + "," + clickedColumn);
if (result.IsConnected)
{
MessageBox.Show("Success!");
}
}
}
}
和API:
public class GameAPI
{
public IGame CurrentGame { get; set; }
public void CreateGame(GameType type)
{
CurrentGame = new SinglePlayerGame();
}
public Result Move(int column)
{
if (CurrentGame == null) return null;
Player player = CurrentGame.GetNextPlayer();
if (player.Type == PlayerType.Human) return CurrentGame.Move(column, player.Id);
}
public Result MoveBot()
{
// Simulate Bot's move...
}
}
My first thought was to create an event in GameAPI class. Is that good idea?
是的,为什么不呢?让我们以现代 UI 框架数据绑定为例。使数据绑定工作的关键点是在修改对象的某些 属性 值时提供 属性 更改通知(读取 - 事件)。通常这是通过 IPropertyNotifyChanged
接口实现的,这只是声明支持 PropertyChanged
event 的一种多态方式。这样,如果您通过代码设置对象 属性,UI 会自动更新。希望您看到与您的案例的相似之处 - API 做了一些事情并引发了一个事件,UI(作为较早的一点附加到该事件的处理程序)接收事件并相应地更新。