急需Xamarin.IOS模态MessageBox类弹出窗口

急需Xamarin.IOS模态MessageBox类弹出窗口,xamarin.ios,uipopovercontroller,uialertcontroller,Xamarin.ios,Uipopovercontroller,Uialertcontroller,在Xamarin IOS中编码。我有一个下拉列表类型弹出窗口,如果最终用户键入新值,我想问一个是/否问题:是否要添加新行 控件位于UIStackView中,UIStackView位于容器UIView中,而容器UIView又位于通过segue显示的另一个容器UIView中。Xamarin要求使用我实现的UIPopoverController。以下是我目前掌握的代码: using System.Threading.Tasks; using Foundation; using UIKit; name

在Xamarin IOS中编码。我有一个下拉列表类型弹出窗口,如果最终用户键入新值,我想问一个是/否问题:是否要添加新行

控件位于UIStackView中,UIStackView位于容器UIView中,而容器UIView又位于通过segue显示的另一个容器UIView中。Xamarin要求使用我实现的UIPopoverController。以下是我目前掌握的代码:

using System.Threading.Tasks;
using Foundation;
using UIKit;

namespace PTPED_Engine
{

    public enum MessagePopupType
    {
        YesNo = 1,
        OKCancel = 2,
        OKOnly = 3
    }

    public enum PopupResultType
    {
        OK = 1,
        Cancel = 2,
        Yes = 3,
        No = 4
    }

    public static class AlertPopups
    {
        static NSObject nsObject;

        public static void Initialize(NSObject nsObject)
        {
            AlertPopups.nsObject = nsObject;
        }
        public static Task<PopupResultType> AskUser(UIViewController parent, UIView V, string strTitle, string strMsg, MessagePopupType mpt)
        {
            using (UIPopoverController pc = new UIPopoverController(parent))
            {
               // pc.ContentViewController
                // method to show an OK/Cancel dialog box and return true for OK, or false for cancel
                var taskCompletionSource = new TaskCompletionSource<PopupResultType>();

                var alert = UIAlertController.Create(strTitle, strMsg, UIAlertControllerStyle.ActionSheet);
                // set up button event handlers
                if (mpt == MessagePopupType.OKCancel)
                {
                    alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, a => taskCompletionSource.SetResult(PopupResultType.OK)));
                    alert.AddAction(UIAlertAction.Create("Cancel", UIAlertActionStyle.Default, a => taskCompletionSource.SetResult(PopupResultType.Cancel)));
                }
                if (mpt == MessagePopupType.YesNo)
                {
                    alert.AddAction(UIAlertAction.Create("Yes", UIAlertActionStyle.Default, a => taskCompletionSource.SetResult(PopupResultType.Yes)));
                    alert.AddAction(UIAlertAction.Create("No", UIAlertActionStyle.Default, a => taskCompletionSource.SetResult(PopupResultType.No)));
                }
                if (mpt == MessagePopupType.OKOnly)
                {
                    alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, a => taskCompletionSource.SetResult(PopupResultType.OK)));
                }
                // show it
                nsObject.InvokeOnMainThread(() =>
                {
                    pc.PresentFromRect(V.Bounds, V, UIPopoverArrowDirection.Any, true);
                });

                return taskCompletionSource.Task;
            }
        }
    }
}
这没用。它挂起用户界面

这应该是内置的,但它只内置在Xamarin.Forms中,我不想使用它

我在等待中做这件事没有问题,但这根本不起作用。有人能帮忙吗


谢谢

这是我几年前提供的一个解决方案,与您优雅的方法相比,我认为这是一个丑陋的破解。你没有说哪部分不起作用,这可能有助于发现问题

以下是我几年前的解决方案:


您可以使用ACR用户对话框库:

我没有看到任何东西将您的警报链接到PoOver。您可以使用我在调用之前添加的UserDialogs库:pc.ContentViewController=alert;没有效果。这是一个非常聪明的方法!我从来没有想过以这种方式使用wait来进行模态对话。也就是说,你说“根本不工作”哪部分不工作?UI永远不会显示,或者它永远不会消失?几年前,我在stackoverflow中提供了一个丑陋的替代方案,它会增加事件队列,这不是一件好事,我更喜欢你的方法。pnavk请将你的评论作为答案发布,以便我可以接受。Acr对话框优雅美观,第一次就完美地工作了。谢谢
LookupCombo.Completed += async (object sender, CompletedEventArgs e) =>
{
    C1AutoComplete AC = (C1AutoComplete)sender;
    if (AC.Text.Trim() != "")
    {
        string sColName = AC.AccessibilityIdentifier.Trim();
        var ValuesVC = (List<Lookupcombo_Entry>)AC.ItemsSource;
        var IsThisAHit = from Lookupcombo_Entry in ValuesVC
                         where Lookupcombo_Entry.sDispVal.ToUpper().Trim() == e.value.ToUpper().Trim()
                         select Lookupcombo_Entry.sMapVal;
        if (!IsThisAHit.Any())
        {
            string sTitle = "";
            string sFull = _RM.GetString(sColName);
            if (sFull == null) { sFull = "???-" + sColName.Trim(); }
            sTitle = " Add New " + sFull.Trim() + "?";
            string sPPrompt = "Do you want to add a new " + sFull.Trim() + " named " + AC.Text.Trim() + " to the Database?";
            var popupResult = await AlertPopups.AskUser(CurrentViewController(), V, sTitle, sPPrompt, MessagePopupType.YesNo);
          }
    }
};
    private UIViewController CurrentViewController()
    {
        var window = UIApplication.SharedApplication.KeyWindow;
        var vc = window.RootViewController;
        while (vc.PresentedViewController != null)
        {
            vc = vc.PresentedViewController;
        }
        return vc;
    }