0
我有一個Dispatcher,我必須用它來更新我的位圖,但是返回行是在調度程序內部的代碼完成之前執行的,所以結果並不如預期。在Dispatcher.invoke方法返回後
如何在調度員完成後返回一個值?我不能離開調度員的回報。
下面是我的代碼:
public ObservableCollection<FavoriteStruct> LoadMobion()
{
List<FavoriteMobion> results;
using (FavoriteDataContext context = new FavoriteDataContext(connectionString))
{
IQueryable<FavoriteMobion> query = from c in context.TbFavoriteMobion
select c;
results = query.ToList();
}
ObservableCollection<FavoriteStruct> lstFavStr = new ObservableCollection<FavoriteStruct>();
if (results != null)
{
Thread load = new Thread(new ThreadStart(() =>
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
foreach (var rst in results)
{
BitmapImage bi;
bi = new BitmapImage();
bi.UriSource = new Uri("../Images/default_avatar_small.jpg", UriKind.Relative);
lstFavStr.Add(new FavoriteStruct()
{
Opacity = 0.8f,
Status = "../Images/offline.png",
RealID = rst.RealID,
GroupType = rst.GroupType,
MsgStatus = rst.MsgStatus == "" ? "offline" : rst.Path,
Name = rst.Name,
Path = rst.Path == "" ? "" : rst.Path,
Phone = rst.Phone,
Picture = bi
});
}
});
}));
load.Start();
load.Join();
}
return lstFavStr;
}