Silverlight MVVM 异步数据未载入完便返回空数据
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; }
void LoadUserList() { DataService ds = new DataService(); this.UserList= new List<User>(); MessageBox.Show("1"); this.UserList= ds.GetAllUser(); MessageBox.Show("3"); }
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>(); 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); } callback(userList); }; }