等待PushModalAsync表单以xamarin形式关闭

时间:2016-09-23 04:59:36

标签: c# xamarin async-await xamarin.forms

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

从弹出页面用户

可以添加新条目或取消/关闭窗口而无需执行任何操作

一切正常,代码就像这样

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

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

4 个答案:

答案 0 :(得分:12)

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

答案 1 :(得分:4)

这里的答案有点晚了,但最好是听一下应用程序的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,在模式从导航堆栈中完全弹出后调用。如果使用它,它应该类似。

答案 2 :(得分:2)

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

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";

    }
}

希望得到这个帮助。

答案 3 :(得分:2)

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

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

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