Can constructors be async?

后端 未结 12 1121
迷失自我
迷失自我 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

    I use this easy trick.

    public sealed partial class NamePage
    {
      private readonly Task _initializingTask;
    
      public NamePage()
      {
        _initializingTask = Init();
      }
    
      private async Task Init()
      {
        /*
        Initialization that you need with await/async stuff allowed
        */
      }
    }
    

提交回复
热议问题