我正在以 xamarin 形式创建一个应用程序。我正在使用“显示警报”来显示带有“是/否”按钮的消息。但是当用户点击弹出窗口外部时,它就会接近。我不想关闭外部水龙头上的弹出窗口。仅当用户单击"is"或“否”按钮后,它才应关闭。
如何管理我的弹出窗口,以便当用户在 xamarin 表单中点击弹出窗口外部时它无法关闭?
最佳答案
这可能有点太晚了,但它可能会让遇到这个问题的其他人受益。
一旦您知道如何做,这可以很容易地完成。下面是一个简短的分步指南,可以帮助您运行。
- 在共享项目中创建一个新的类文件,并将其命名为CustomYesNoBox.cs。
在此文件中,粘贴以下代码:
using System; using System.Collections.Generic; using System.Linq; using Xamarin.Forms; public class CustomYesNoBox { public string Text { get; set; } public string Title { get; set; } public List<string> Buttons { get; set; } public CustomYesNoBox(string title, string text, params string[] buttons) { Title = title; Text = text; Buttons = buttons.ToList(); } public CustomYesNoBox(string title, string text):this(title, text, "Yes", "No") { } public event EventHandler<CustomYesNoBoxClosedArgs> PopupClosed; public void OnPopupClosed(CustomYesNoBoxClosedArgs e) { var handler = PopupClosed; if (handler != null) handler(this, e); } public void Show() { DependencyService.Get<IYesNoPopupLoader>().ShowPopup(this); } } public class CustomYesNoBoxClosedArgs : EventArgs { public string Button { get; set; } } public interface IYesNoPopupLoader { void ShowPopup(CustomYesNoBox reference); }
在您的 Android 项目文件夹中,添加一个新类,并将其命名为 YesNoPopupLoader.cs。
在此文件中,粘贴以下代码:
using Android.App; using Android.Widget; using <YOUR_NAMESPACE>.Droid.Implementation; using Xamarin.Forms; [assembly: Dependency(typeof(YesNoPopupLoader))] namespace <YOUR_NAMESPACE>.Droid.Implementation { public class YesNoPopupLoader : IYesNoPopupLoader { public void ShowPopup(CustomYesNoBox popup) { var alert = new AlertDialog.Builder(Forms.Context); var textView = new TextView(Forms.Context) { Text = popup.Text }; alert.SetView(textView); alert.SetTitle(popup.Title); var buttons = popup.Buttons; alert.SetPositiveButton(buttons[0], (senderAlert, args) => { popup.OnPopupClosed(new CustomYesNoBoxClosedArgs { Button = buttons[0] }); }); alert.SetNegativeButton(buttons[1], (senderAlert, args) => { popup.OnPopupClosed(new CustomYesNoBoxClosedArgs { Button = buttons[1] }); }); alert.SetCancelable(false); alert.Show(); } } }
在您的 iOS 项目中,添加一个新的类文件,并将其命名为 YesNoPopupLoader.cs。
在此文件中,粘贴以下代码:
using System; using System.Linq; using <YOUR_NAMESPACE>.iOS.Implementation; using UIKit; [assembly: Xamarin.Forms.Dependency(typeof(YesNoPopupLoader))] namespace <YOUR_NAMESPACE>.iOS.Implementation { public class YesNoPopupLoader : IYesNoPopupLoader { public void ShowPopup(CustomYesNoBox popup) { var alert = new UIAlertView { Title = popup.Title, Message = popup.Text }; foreach (var b in popup.Buttons) alert.AddButton(b); alert.Clicked += (s, args) => { popup.OnPopupClosed(new CustomYesNoBoxClosedArgs { Button = popup.Buttons.ElementAt(Convert.ToInt32(args.ButtonIndex)), }); }; alert.Show(); } } }
返回您的共享项目,然后转到您想要访问消息框的任何位置。在此示例中,我的 MainPage 中有一个按钮将提示此消息框:
YesNoButton.Clicked += (sender, e) => { var popup = new CustomYesNoBox("Question", "Is this Android?"); popup.PopupClosed += (o, closedArgs) => { if (closedArgs.ButtonText == "Yes") { // Do something on positive response } else if(closedArgs.ButtonText == "No") { // Do something on negative response } else { // Unknown response. Do nothing? } }; popup.Show(); };
应该就是这样。您可以尝试创建 Xamarin Forms 不提供的其他自定义消息框(带有下拉菜单、文本输入、进度指示器等),或者自定义消息框的布局。
关于android - 触摸外部时取消 DisplayAlert,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40169056/