等待 PushModalAsync 表单以 xamarin 形式关闭

本文关键字:xamarin PushModalAsync 表单 等待 | 更新日期: 2023-09-27 18:08:29

我有一个页面,单击工具栏上的加号按钮时,我正在调用一个弹出页面

从弹出页面用户可以添加新条目或取消/关闭窗口而无需执行任何操作

一切正常,代码是这样的

    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();
        }
    }

但是现在我想等待弹出窗口关闭以进行一些额外的编码,我尝试了以下代码

 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);
            }

但是等待不起作用.我怎么能在这个区域等待直到弹出窗口关闭?知道吗??

等待 PushModalAsync 表单以 xamarin 形式关闭

使用模态页面上的 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");

这里的答案有点晚了,但最好侦听应用程序的OnModalPagePopping事件处理程序。

首先,创建模式页面。为其提供一个属性来存储以后要检索的数据:

public class MyModalPage : ContentPage
{
    public string Data { get; set; }
    public MyModalPage()
    {
        InitializeComponent();
        // ... set up the page ...
    }
    private async void PopThisPage()
    {
        // When you want to pop the page, just call this method
        // Perhaps you have a text view with x:Name="PhoneNumber", for example
        Data = PhoneNumber.Text; // store the "return value" before popping
        await MyProject.App.Current.MainPage.Navigation.PopModalAsync();
    }
}

父页面中,可以创建模式页,并设置事件处理程序以在模式页弹出时侦听:

public class MyPage : ContentPage
{
    MyModalPage _myModalPage;
    public MyPage()
    {
        InitializeComponent();
        // ... set up the page ...
    }
    private async void ShowModalPage()
    {
        // When you want to show the modal page, just call this method
        // add the event handler for to listen for the modal popping event:
        MyProject.App.Current.ModalPopping += HandleModalPopping;
        _myModalPage = new MyModalPage();
        await MyProject.App.Current.MainPage.Navigation.PushModalAsync(_myModalPage());
    }
    private void HandleModalPopping(object sender, ModalPoppingEventArgs e)
    {
        if (e.Modal == _myModalPage)
        {
            // now we can retrieve that phone number:
            var phoneNumber = _myModalPage.Data;
            _myModalPage = null;
            // remember to remove the event handler:
            MyProject.App.Current.ModalPopping -= HandleModalPopping;
        }
    }
}

这比使用 OnDisappearing 方法更好,因为其他人已经说过可以在应用程序后台时调用,等等。而且它的行为在各个平台上并不一致。

还有另一个事件OnModalPopped,它是在模态完全从导航堆栈中弹出后调用的。如果使用它,它应该类似地工作。

您可以尝试创建一个事件,在弹出关闭时调用。

public partial class AddSchool : ContentPage
{
    public delegate void PopupClosedDelegate();
    public event PopupClosedDelegate PopupClosed;
    public AddSchool()
    {
        InitializeComponent();
    }
    private async void Button_OK_Clicked(object sender, EventArgs e)
    {
        //doing some operations like entry to db etc and close page
        await Navigation.PopModalAsync();
        if (PopupClosed!=null)
        {
            PopupClosed();
        }
    }
    private async void cancelClicked(object sender, EventArgs e)
    {
        await Navigation.PopModalAsync();
        if (PopupClosed != null)
        {
            PopupClosed();
        }
    }
}

我把它放在按钮点击事件上,也许你可以把它放在关闭或处置事件上。那么这里是实现

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();
                target_page.PopupClosed += () => { /*Do something here*/ };
                Navigation.PushModalAsync(target_page);
            }, 0, 0);
        }
        ToolbarItems.Add(tbi);
        #endregion
        this.Title = "Select School";
    }
}

希望这有帮助。

我为此创建了一个扩展方法:

public static class DialogUtils
{
    public static async Task ShowPageAsDialog(this INavigation navigation, Page page)
    {
        int pagesOnStack = navigation.NavigationStack.Count + 1;
        var waitHandle = new EventWaitHandle(false, EventResetMode.AutoReset);
        page.Disappearing += (s, e) =>
        {
            if (navigation.NavigationStack.Count <= pagesOnStack)
                waitHandle.Set();
        };
        await navigation.PushAsync(page);
        await Task.Run(() => waitHandle.WaitOne());
    }
}

我可以使用它:

private async void bShowDialogPage_Clicked(object sender, EventArgs e)
{
    var page = new DialogPage();
    await page.LoadData();
    await Navigation.ShowPageAsDialog(page);
    var result = page.PageResult;
}

它支持对话框页面显示另一个页面的情况。我更喜欢NavigationStack而不是ModalStack,因为NavigationPage和BackButton。

实现此目的的另一种方法是从页面的 OnDisapearing 方法调用事件,然后可以由您创建的导航服务订阅此事件,然后您可以使用"TaskCompletionSource" wati,直到页面完成其工作,然后完成任务。有关完成此操作的更多详细信息,您可以查看此博客文章。

以下是基本页面的实现,此演示应用中的每个页面都继承此页面:

public class BasePage<T> : ContentPage
{
    public event Action<T> PageDisapearing;
    protected T _navigationResut;
    public BasePage()
    {
    }
    protected override void OnDisappearing()
    {
        PageDisapearing?.Invoke(_navigationResut);
        if (PageDisapearing != null)
        {
            foreach (var @delegate in PageDisapearing.GetInvocationList())
            {
                PageDisapearing -= @delegate as Action<T>;
            }
        }
        base.OnDisappearing();
    }
}

以下是应使用的导航服务的概述:

public async Task<T> NavigateToModal<T>(string modalName)
    {
        var source = new TaskCompletionSource<T>();
        if (modalName == nameof(NewItemPage))
        {
            var page = new NewItemPage();
            page.PageDisapearing += (result) =>
            {
                var res = (T)Convert.ChangeType(result, typeof(T));
                source.SetResult(res);
            };
            await App.Current.MainPage.Navigation.PushModalAsync(new NavigationPage(page));
        }
        return await source.Task;
    }

若要使用导航服务调用此页,可以使用以下代码:

 var item = await new SimpleNavigationService().NavigateToModal<Item>(nameof(NewItemPage));
        Items.Add(item);