我的View中有一个ListBox,它在我的ViewModel中DataBinded到一个ObservableCollection。但是,即使我在ViewModel中加载数据,我的View也没有更新
我使用此委托加载列表的数据:
public ObservableCollection<TwitterElement_> MyCollection_
{
get;
private set;
}
[...]
public MainViewModel()
{
this.MyList_ = new List<Object>();
this.MyCollection_ = new ObservableCollection<Object>();
this.loadedCommand = new RelayCommand(async() =>
{
this.MyList_= await FirstLoad();
foreach (Element_ t in MyList_)
{
this.MyCollection_.Add(t);
}
});
}
public async Task<List<Object>> FirstLoad()
{
var tcs = new TaskCompletionSource<List<Object>>();
var client = new RestSharp.RestClient("https://Example.com");
var request = new RestSharp.RestRequest("/request", Method.GET);
client.ExecuteAsync(request, (response) =>
{
if (response.StatusCode == HttpStatusCode.OK)
{
Task<List<Object>> FirstTask = Task.Factory.StartNew<List<Object>>(() => JsonConvert.DeserializeObject<List<Object>>(response.Content));
Task<List<Object>> SecondTask = FirstTask.ContinueWith((ft) =>
{
//Operations That I removed for more clarity...
[...]
return ft.Result;
});
tcs.SetResult(SecondTask.Result);
}
else
{
}
});
return await tcs.Task;
}
但是我的ListBox没有使用数据进行更新,即使我将对象从List传输到应该作用于UI线程的ObservableCollection我该怎么办?
答案 0 :(得分:0)
您的收藏是否已绑定到ListBox
Binding
,或者您是否手动设置ItemsSource
属性?
如果使用Binding
完成,则需要将DataContext
页面的ListBox
设置为您的视图模型。 DataContext = [instance of MainViewModel]