我正在试图弄清楚如何执行标题中提到的内容。我将在下面提供我试图实现的代码样本。尽管如此,我需要能够等待页面加载,然后在页面上等待单击按钮的操作,然后才能继续执行页面等待下面的代码。
目前,如果我执行类似下面的代码,它会加载页面A,然后继续加载页面B,因为我不知道如何让它等待页面A上的操作
//do some stuff in here
//then push page "A"
//await page "A" to load
//but also await for button
//"A" to be pressed
await DoStuffThenPushPageA()
//DO NOT EXECUTE DostuffThenPushPageB() until "A" is pressed
//do some stuff in here
//then push page "B"
//await page "B" to load
//but also await for button
//"B" to be pressed
await DostuffThenPushPageB()
发布于 2020-06-11 11:16:54
您可以在PageA中编写event,并在PageA中的操作完成时通知MainPage,然后执行DostuffThenPushPageB
在MainPage中:
public MainPage()
{
InitializeComponent();
goToPage1Async();
}
public async void goToPage1Async()
{
//DO STUFF
Console.WriteLine("dostuff and go to Page1");
Page1 page1 = new Page1();
page1.myActionFinish += Page1_myActionFinish1;
await Navigation.PushAsync(page1);
}
private void Page1_myActionFinish1(object sender, EventArgs e)
{
//this method will be executed when doMyStuff in Page1 completed
// then go to page2
Console.WriteLine("start to go to Page2");
Page2 page2 = new Page2();
//await Navigation.PushAsync(page2);
}在Page1中:
public partial class Page1 : ContentPage
{
public event EventHandler myActionFinish;
public Page1()
{
InitializeComponent();
}
protected override void OnAppearing()
{
base.OnAppearing();
doMyStuff();
}
private void doMyStuff()
{
Console.WriteLine("doMyStuff in Page1");
//If the stuff has been finsihed, notify MainPage to startPage2
if (myActionFinish != null)
{
myActionFinish(this, EventArgs.Empty);
}
}
}发布于 2020-06-11 05:54:41
你可以让你在按钮上的点击是异步的:
private async void ButtonA_Click(object sender, RoutedEventArgs e)
{
await SomeStuff();
}我猜您希望用户按照给定的顺序执行操作
https://stackoverflow.com/questions/62313283
复制相似问题