1
我想复制现有的.NET XNA类的事件提供的功能:NetworkSession.GamerJoined。如何在分配新处理程序时引发事件?
当事件被分配给它可以立即得到提升,例如:
myNetworkSession.GamerJoined += GamerJoinedEventHandler;
void GamerJoinedEventHandler(object sender, GamerJoinedEventArgs e)
{
// If you put a beakpoint on the assignment line above and another here
// this handler is called during the assignment.
}
我怎样才能做到这一点,如果我写我自己的NetworkSession
类的GamerJoined
事件?
如果您在事件处理程序方法中使用了副作用,那么只需直接调用它,则不需要XNA来为您执行此操作。很可能你必须重构它并将一些代码移入私有方法,因为构建有效的GamerJoinedEventArgs实例应该很困难。 –
@HansPassant我正在编写这个类的实现,所以为XNA编写的用户代码只能使用我的一个。 – markmnl
然后它是一种代码味道。 *某些*客户端代码有兴趣知道玩家何时加入网络,您不知道代码是什么样子,*不*表示游戏玩家实际加入。 –