데이터를 처리하고 처리 된 데이터를보기로 전달하는 경우 아래 옵션이 하나의 가능한 솔루션이어야한다고 생각합니다.
보기는 동시에 변경 사항을 통보하는 동안 데이터를 처리합니다.
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 외부의 다른 클래스에서 정의 할 수 있습니다.
이 정보가 도움이되기를 바랍니다.