3 回答
TA贡献1789条经验 获得超8个赞
asyncvoidTask.
TTask<T>
async void
async voidasync void
TA贡献1831条经验 获得超9个赞
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贡献1895条经验 获得超3个赞
Initialize/OpenInitialize
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 关注
- 666 浏览
添加回答
举报
