xamarin.forms - 如何暂停方法直到从 Xamarin.Forms 中的 Rg.Plugins.Popup 获得响应?

标签 xamarin.forms popup

我使用 Rg.Plugins.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);
        }

最佳答案

您可以使用 TaskCompletionSource

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

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

关于xamarin.forms - 如何暂停方法直到从 Xamarin.Forms 中的 Rg.Plugins.Popup 获得响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45791902/

相关文章:

android - 如果已执行,则禁用一个按钮

ios - 如何阅读崩溃日志并在 Xamarin.forms 的 iOS 上找到线索?

asp.net - 如何从android模拟器访问本地主机?

xamarin.forms - 如何获取导航栏背景颜色?

c# - Http.HttpRequestException Xamarin.forms

javascript - jQuery Mobile popup data-dismissible ="false"问题

c# - Xamarin.Forms 中的富文本框

c# - Windows 10 上的 ContextMenu 和 Popup WPF 控件对齐错误

c# - 如何在 WinRT 中同时允许多个弹出窗口?

JQuery 弹出窗口出现在某些 map 元素下