在线演示地址:Silverlight+WCF 新手实例 象棋 在线演示
上节是当有用户进入某个房间时,我的某个房间状态被通知,并被通知更新。
这节说说首次进入房间大厅时,我们自己创建了N个房间,默认都是初始状态的,这时我们需要获取服务端的所有已更新的房间状态,
下到本地之后,进行批量更新状态。
于是开始了,首先从服务端开始,我们要获取所有已更新的房间,于是到WCF服务端添加一个方法:
到IService.cs添加方法接口
[OperationContract]
Dictionary<int, Room> GetRoomList();
Dictionary<int, Room> GetRoomList();
接着要实现方法了,到Service.cs去
太阳,这方法太简单了,由于我们之前就有全局的roomList对象,直接返回就可以了。
public Dictionary<int, Room> GetRoomList()
{
return roomList;
}
{
return roomList;
}
两三行代码就搞定了服务端了。于是我们悄悄的回到客户端
当然了,还是得编绎,更新服务引用,这个说多了,大伙自觉点。
我们回到房间页面Room.xaml.cs里去
我们在构造函数里添加两行代码,默认请求房间列表:
由于之前已有些代码,大伙看注释那两行:
public Room()
{
InitializeComponent();
game = new Game();
game.CreateGameRoom(30);
game.DrawIn(LayoutRoot);
//看这里看这里,这两行是新添加的,获取房间列表
App.client.GetRoomListCompleted += new EventHandler<GameService.GetRoomListCompletedEventArgs>(client_GetRoomListCompleted);
App.client.GetRoomListAsync();
//这里实现ICallBack的方法
App.client.NotifyRoomUpdateReceived += new EventHandler<GameService.NotifyRoomUpdateReceivedEventArgs>(client_NotifyRoomUpdateReceived);
}
//这里也要看,这里是获取房间列表的事件
void client_GetRoomListCompleted(object sender, GameService.GetRoomListCompletedEventArgs e)
{
//房间获取完了,待实现
}
{
InitializeComponent();
game = new Game();
game.CreateGameRoom(30);
game.DrawIn(LayoutRoot);
//看这里看这里,这两行是新添加的,获取房间列表
App.client.GetRoomListCompleted += new EventHandler<GameService.GetRoomListCompletedEventArgs>(client_GetRoomListCompleted);
App.client.GetRoomListAsync();
//这里实现ICallBack的方法
App.client.NotifyRoomUpdateReceived += new EventHandler<GameService.NotifyRoomUpdateReceivedEventArgs>(client_NotifyRoomUpdateReceived);
}
//这里也要看,这里是获取房间列表的事件
void client_GetRoomListCompleted(object sender, GameService.GetRoomListCompletedEventArgs e)
{
//房间获取完了,待实现
}