如何从构造函数调用异步方法?
How can I call async method from constructor?
我需要从 Form1
构造函数中调用 async
方法。由于构造函数不能具有 return 类型,因此我无法添加 async void
。我读到 static constructor 可以是 async
但我需要从不是 static
的构造函数调用方法,例如 InitializeComponent()
(因为它是表单的构造函数)。
class是:
public partial class Form1 : Form
{
InitializeComponent();
//some stuff
await myMethod();
}
我也读了 this 一个,但我仍然不知道如何实现它(在我的情况下),因为该方法仍然需要使用 async
.
不要在构造函数中执行此操作,而是在 window 的加载事件中执行此操作。
您可以将加载的事件处理程序标记为异步。
您可以使用静态方法 returns 您的表单实例
public class TestForm : Form
{
private TestForm()
{
}
public static async Task<TestForm> Create()
{
await myMethod();
return new TestForm();
}
}
Task.Run(async () => await YourAsyncMethod());
我的示例是从页面构造函数调用学生详细信息
1-导航页的调用
void Handle_ItemTapped(object sender, Xamarin.Forms.ItemTappedEventArgs e)
{
Student _student = (Student)e.Item;
Navigation.PushAsync(new Student_Details(_student.ID));
}
2 - 详情页
public partial class Student_Details : ContentPage
{
public Student_Details(int id)
{
InitializeComponent();
Task.Run(async () => await getStudent(id));
}
public async Task<int> getStudent(int id)
{
Student _student;
SQLiteDatabase db = new SQLiteDatabase();
_student = await db.getStudent(id);
return 0;
}
}
虽然一般建议您通常不应该在构造函数中执行此操作,但您可以执行以下操作,我已经在应用程序(例如控制台应用程序)中使用过,我需要在其中调用一些现有的异步代码:
DetailsModel details = null; // holds the eventual result
var apiTask = new Task(() => details = MyService.GetDetailsAsync(id).Result); // creates the task with the call on another thread
apiTask.Start(); // starts the task - important, or you'll spin forever
Task.WaitAll(apiTask); // waits for it to complete
Philip 是正确的,如果你可以避免在构造函数中这样做,你应该。
我需要从 Form1
构造函数中调用 async
方法。由于构造函数不能具有 return 类型,因此我无法添加 async void
。我读到 static constructor 可以是 async
但我需要从不是 static
的构造函数调用方法,例如 InitializeComponent()
(因为它是表单的构造函数)。
class是:
public partial class Form1 : Form
{
InitializeComponent();
//some stuff
await myMethod();
}
我也读了 this 一个,但我仍然不知道如何实现它(在我的情况下),因为该方法仍然需要使用 async
.
不要在构造函数中执行此操作,而是在 window 的加载事件中执行此操作。 您可以将加载的事件处理程序标记为异步。
您可以使用静态方法 returns 您的表单实例
public class TestForm : Form
{
private TestForm()
{
}
public static async Task<TestForm> Create()
{
await myMethod();
return new TestForm();
}
}
Task.Run(async () => await YourAsyncMethod());
我的示例是从页面构造函数调用学生详细信息
1-导航页的调用
void Handle_ItemTapped(object sender, Xamarin.Forms.ItemTappedEventArgs e)
{
Student _student = (Student)e.Item;
Navigation.PushAsync(new Student_Details(_student.ID));
}
2 - 详情页
public partial class Student_Details : ContentPage
{
public Student_Details(int id)
{
InitializeComponent();
Task.Run(async () => await getStudent(id));
}
public async Task<int> getStudent(int id)
{
Student _student;
SQLiteDatabase db = new SQLiteDatabase();
_student = await db.getStudent(id);
return 0;
}
}
虽然一般建议您通常不应该在构造函数中执行此操作,但您可以执行以下操作,我已经在应用程序(例如控制台应用程序)中使用过,我需要在其中调用一些现有的异步代码:
DetailsModel details = null; // holds the eventual result
var apiTask = new Task(() => details = MyService.GetDetailsAsync(id).Result); // creates the task with the call on another thread
apiTask.Start(); // starts the task - important, or you'll spin forever
Task.WaitAll(apiTask); // waits for it to complete
Philip 是正确的,如果你可以避免在构造函数中这样做,你应该。