首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 媒体动画 > CAD教程 >

Silverlight MVVM 异步数据未载入完就返回空数据

2012-10-09 
Silverlight MVVM 异步数据未载入完便返回空数据Services\DataService.csC# codepublic ListUser GetAll

Silverlight MVVM 异步数据未载入完便返回空数据
Services\DataService.cs

C# code
        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
C# code
void LoadUserList()        {            DataService ds = new DataService();            this.UserList= new List<User>();            MessageBox.Show("1");            this.UserList= ds.GetAllUser();            MessageBox.Show("3");        }


弹出"1"后直接出"3",再出"2"...

请教大家用什么方法解决?

[解决办法]
异步本来就是这样的,跟同步编程是两回事。

程序的接口需要做出根本性的改变:
C# code
        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);            };        } 

热点排行