3 回答
TA贡献1804条经验 获得超3个赞
async
void
Task
.
T
Task<T>
async void
async void
async void
TA贡献1876条经验 获得超5个赞
public class ViewModel { public ObservableCollection<TData> Data { get; set; } //static async method that behave like a constructor async public static Task<ViewModel> BuildViewModelAsync() { ObservableCollection<TData> tmpData = await GetDataTask(); return new ViewModel(tmpData); } // private constructor called by the async method private ViewModel(ObservableCollection<TData> Data) { this.Data=Data; } }
TA贡献2041条经验 获得超4个赞
Initialize
/Open
Initialize
Initialize()
Initialize()
Initialize()
Initialize
public MyClass{ public static async Task<MyClass> CreateAsync(...) { MyClass x = new MyClass(); await x.InitializeAsync(...) return x; } // make sure no one but the Create function can call the constructor: private MyClass(){} private async Task InitializeAsync(...) { // do the async things you wanted to do in your async constructor } public async Task<int> OtherFunctionAsync(int a, int b) { return await OtherFunctionAsync(a, b); }
public async Task<int> SomethingAsync(){ // Create and initialize a MyClass object MyClass myObject = await MyClass.CreateAsync(...); // use the created object: return await myObject.OtherFunctionAsync(4, 7);}
- 3 回答
- 0 关注
- 634 浏览
添加回答
举报