I have a project where I\'m trying to populate some data in a constructor:
public class ViewModel
{
public ObservableCollection Data { get;
Since it is not possible to make an async constructor, I use a static async method that returns a class instance created by a private constructor. This is not elegant but it works ok.
public class ViewModel
{
public ObservableCollection Data { get; set; }
//static async method that behave like a constructor
async public static Task BuildViewModelAsync()
{
ObservableCollection tmpData = await GetDataTask();
return new ViewModel(tmpData);
}
// private constructor called by the async method
private ViewModel(ObservableCollection Data)
{
this.Data = Data;
}
}