gpt4 book ai didi

c# - 等待 PushModalAsync 表单以 xamarin 表单关闭

转载 作者:太空狗 更新时间:2023-10-29 20:46:31 24 4
gpt4 key购买 nike

我有一个页面,点击工具栏上的加号按钮我调用了一个弹出页面

从弹出页面用户可以添加新条目或取消/关闭窗口而不做任何事情

一切正常,代码是这样的

    public partial class SelectSchool : ContentPage
{
public SelectSchool()
{
InitializeComponent();
#region toolbar
ToolbarItem tbi = null;
if (Device.OS == TargetPlatform.Android)
{
tbi = new ToolbarItem("+", "plus", async () =>
{
var target_page = new AddSchool();
Navigation.PushModalAsync(target_page);
}, 0,0);
}
ToolbarItems.Add(tbi);
#endregion

this.Title = "Select School";

}
}

我的弹出页面是这样的

     public partial class AddSchool : ContentPage
{
public AddSchool()
{
InitializeComponent();
}
private async void Button_OK_Clicked(object sender, EventArgs e)
{
//doing some operations like entry to db etc and close page
Navigation.PopModalAsync();

}
private void cancelClicked(object sender, EventArgs e)
{
Navigation.PopModalAsync();
}
}

但现在我想等待 Popup 关闭来做一些额外的编码,我尝试了下面的代码

 if (Device.OS == TargetPlatform.Android)
{
tbi = new ToolbarItem("+", "plus", async () =>
{
var target_page = new AddSchool();
await Navigation.PushModalAsync(target_page);
//await till target_page is closed and once its closed call my next function here
}, 0,0);
}

但是 await 不起作用。我怎样才能在这个区域等待直到弹出窗口关闭?有什么想法吗??

最佳答案

在模态页面上使用 Disappearing 事件。

示例:

var modalPage = new ContentPage();
modalPage.Disappearing += (sender2, e2) =>
{
System.Diagnostics.Debug.WriteLine("The modal page is dismissed, do something now");
};
await content.Navigation.PushModalAsync(modalPage);
System.Diagnostics.Debug.WriteLine("The modal page is now on screen, hit back button");

或者使用EventWaitHandle:

var waitHandle = new EventWaitHandle(false, EventResetMode.AutoReset);
var modalPage = new ContentPage();
modalPage.Disappearing += (sender2, e2) =>
{
waitHandle.Set();
};
await content.Navigation.PushModalAsync(modalPage);
System.Diagnostics.Debug.WriteLine("The modal page is now on screen, hit back button");
await Task.Run(() => waitHandle.WaitOne());
System.Diagnostics.Debug.WriteLine("The modal page is dismissed, do something now");

关于c# - 等待 PushModalAsync 表单以 xamarin 表单关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39652909/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com