I have a project where I\'m trying to populate some data in a constructor:
public class ViewModel
{
public ObservableCollection Data { get;
Some of the answers involve creating a new public
method. Without doing this, use the Lazy
class:
public class ViewModel
{
private Lazy> Data;
async public ViewModel()
{
Data = new Lazy>(GetDataTask);
}
public ObservableCollection GetDataTask()
{
Task> task;
//Create a task which represents getting the data
return task.GetAwaiter().GetResult();
}
}
To use Data
, use Data.Value
.