Can constructors be async?

后端 未结 12 1252
迷失自我
迷失自我 2020-11-22 09:18

I have a project where I\'m trying to populate some data in a constructor:

public class ViewModel
{
    public ObservableCollection Data { get;          


        
12条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 09:52

    you can use Action inside Constructor

     public class ViewModel
        {
            public ObservableCollection Data { get; set; }
           public ViewModel()
            {              
                new Action(async () =>
                {
                      Data = await GetDataTask();
                }).Invoke();
            }
    
            public Task> GetDataTask()
            {
                Task> task;
                //Create a task which represents getting the data
                return task;
            }
        }
    

提交回复
热议问题