Services\DataService.cs
public List<User> GetAllUser()
{
List<User> userList = new List<User>();
LoadOperation lo = pcs.Load<Web.User>(pcs.GetUsersQuery());
pcs.Load<Web.User>(pcs.GetUsersQuery()).Completed += (s, a) =>
{
foreach (Web.User u in (s as LoadOperation<Web.User>).Entities)
{
User user = new User();
user.Name = u.user_name;
user.Uid = u.user_id;
user.Uiid = u.user_iid;
userList.Add(user);
}
};
MessageBox.Show("2");
return userList;
}
ViewModels\UserMgrViewModel.cs
void LoadUserList()
{
DataService ds = new DataService();
this.UserList= new List<User>();
MessageBox.Show("1");
this.UserList= ds.GetAllUser();
MessageBox.Show("3");
}
弹出"1"后直接出"3",再出"2"...
请教大家用什么方法解决?
------解决方案--------------------------------------------------------
异步本来就是这样的,跟同步编程是两回事。
程序的接口需要做出根本性的改变:
public void GetAllUser(Action<List<User>> callback)
{
LoadOperation lo = pcs.Load<Web.User>(pcs.GetUsersQuery());
pcs.Load<Web.User>(pcs.GetUsersQuery()).Completed += (s, a) =>
{
List<User> userList = new List<User>();