只需在任务执行方法中传递参数即可 new BackgroundTask().execute(a, b, c); // can have any number of params
现在你的背景类应该是这样的 public class BackgroundTask extends AsyncTask<String, Integer, Long> {
@Override
protected Long doInBackground(String... arg0) {
// TODO Auto-generated method stub
String a = arg0[0];
String b = arg0[1];
String c = arg0[2];
//Do the heavy task with a,b,c
return null;
}
//you can keep other methods as well postExecute , preExecute, etc
}
... 展开详请
要标记一个lambda异步,在它的参数列表前加上async: // Add a command to delete the current Group
contextMenu.Commands.Add(new UICommand("Delete this Group", async (contextMenuCmd) =>
{
SQLiteUtils slu = new SQLiteUtils();
await slu.DeleteGroupAsync(groupName);
}));
... 展开详请
由于无法构建异步构造函数,可以使用静态异步方法来返回由私有构造函数创建的类实例。 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;
}
}
... 展开详请