Hi.
I am gathering data in a separate Task and I want to data bind the result to a GUI component using an ObservableCollection<>
. So my code goes something like this:
private ObservableCollection<MyItem> _items;
public ObservableCollection<MyItem> Items
{
get { return _items; }
set
{
if (_items.Equals(value))
{
return;
}
_items = value;
RaisePropertyChanged("Items");
}
}
private void LoadData()
{
Task task = Task.Factory.StartNew(() =>
{
ObservableCollection<MyItem> itms = _htmlParser.FetchData(...);
Dispatcher.CurrentDispatcher.Invoke((Action)delegate
{
Items = itms;
});
});
}
The data is fetched from a component doing some HTTP requests. The error I am getting is:
Must create DependencySource on same Thread as the DependencyObject.
I am using the MVVM Light toolkit framework. I also tried to send the result as a message, but that ended up in the same error message. Any ideas or pointers?
EDIT: Here's the issue:
public class MyItem
{
public string Id { get; set; }
public string Name { get; set; }
public BitmapImage Image { get; set; } // <--- A big No No because it inherits from the DependencyObject
public Uri Uri { get; set; }
}
I changed the BitmapImage
to a byte[]
data type.