如何暂停一个方法,直到从Xamarin.Forms中的Rg.Plugins.Popup获得响应?

时间:2017-08-21 07:55:13

标签: xamarin.forms popup

我正在使用Rg.Plugins.Popup进行简单的确认弹出,然后从列表中删除项目,例如"您确定要从列表中删除Item1吗?"。 我需要知道如何暂停删除方法,直到它从弹出窗口获得确认。

private async void MenuItem_Clicked_1(object sender, EventArgs e)
        {

            var menuItem = sender as MenuItem;
            var item= menuItem.CommandParameter as Item;


            var page = new popupAlert(item);
            await Navigation.PushPopupAsync(page);

           // Pause here
             myList.remove(item);
        }

4 个答案:

答案 0 :(得分:4)

您可以使用TaskCompletionSource

例如,使用异步PopupAlert方法创建Show页面:

public class PopupAlert : PopupPage
{
    TaskCompletionSource<bool> _tcs = null;
    public PopupAlert()
    {
        var yesBtn = new Button { Text = "OK" };
        var noBtn = new Button { Text = "Cancel" };

        yesBtn.Clicked += async (sender, e) =>
        {
            await Navigation.PopAllPopupAsync();
            _tcs?.SetResult(true);
        };
        noBtn.Clicked += async (sender, e) =>
        {
            await Navigation.PopAllPopupAsync();
            _tcs?.SetResult(false);
        };

        Content = new StackLayout
        {
            BackgroundColor = Color.White,
            VerticalOptions = LayoutOptions.Center,
            Padding = 20,
            Children = {
                new Label { Text = "Are you sure you want to delete?" },
                new StackLayout {
                    Orientation = StackOrientation.Horizontal,
                    Children = { yesBtn, noBtn }
                }
            }
        };
    }

    public async Task<bool> Show()
    {
        _tcs = new TaskCompletionSource<bool>();
        await Navigation.PushPopupAsync(this);

        return await _tcs.Task;
    }
}

并且,用法看起来像

private async void MenuItem_Clicked_1(object sender, EventArgs e)
{
    var menuItem = sender as MenuItem;
    var item = menuItem.CommandParameter as Item;

    var popupAlert = new PopupAlert();
    var result = await popup.Show(); //wait till user taps/selects option 

    if(result) //check for user selection here
        myList.remove(item);
}

答案 1 :(得分:3)

为什么不利用MessagingCenter来调用将删除所述项目的方法?

您可以在显示弹出窗口时订阅该消息,并在单击确认按钮时接收该消息。

答案 2 :(得分:0)

您也可以使用DisplayAlert的现有xamarin控件执行相同的操作。

var input= await Application.Current.MainPage.DisplayAlert("Delete Item", "Are You sure you  want to delete this Item?", "Yes","No");
if(input)
{
  myList.remove(item);
} 
else
{
  return;
}

在上面的代码中,如果用户按下yes,则函数返回true,否则返回false。

答案 3 :(得分:0)

我想添加Aziris Morora的答案,这是示例代码(供参考)

//Main Page Constructor
public MainPage()
{
    InitializeComponent();

    MessagingCenter.Subscribe<userDetail>(this, "PopUpData", (value) =>
    {
        string receivedData = value.username;
        usernameLabel.text = receivedData;
    });

}



//PopUp Page Button clicked
private async void OnClickPopUpBtn(object sender, EventArgs e)
{
    await PopupNavigation.PopAsync(true);

    string name = username.Text.ToString();

    MessagingCenter.Send(new userDetail() { username = name }, "PopUpData");
}


//Model Class

class UserDetails
{
    public string username { get; set; }
}