如果您正在处理数据,然后将处理的数据传递给视图,那么我认为下面的选项应该是一个可能的解决方案。
以下解决方案将处理数据,同时视图也会同时通知更改。
public class MyViewModel : INotifyPropertyChanged
{
private ObservableCollection<string> _unprocessedData = new ObservableCollection<string>();
private ObservableCollection<string> _processedData = new ObservableCollection<string>();
private static object _lock = new object();
public event PropertyChangedEventHandler PropertyChanged;
public ObservableCollection<string> Collection { get { return _processedData; } }//Bind the view to this property
public MyViewModel()
{
//Populate the data in _unprocessedData
BindingOperations.EnableCollectionSynchronization(_processedData, _lock); //this will ensure the data between the View and VM is not corrupted
ProcessData();
}
private async void ProcessData()
{
foreach (var item in _unprocessedData)
{
string result = await Task.Run(() => DoSomething(item));
_processedData.Add(result);
//NotifyPropertyChanged Collection
}
}
private string DoSomething(string item)
{
Thread.Sleep(1000);
return item;
}
}
DoSomething方法可以在ViewModel之外的其他类中定义。
我希望这会有所帮助。