gpt4 book ai didi

ios - UIAlertView 不工作是 Xamarin.iOS

转载 作者:行者123 更新时间:2023-11-28 19:29:54 32 4
gpt4 key购买 nike

我针对 Android 和 iOS 的 Xamarin.native 项目。我无法在 Xamarin.iOS 中显示警报;以下代码在 Xamarin.Android 上运行良好。

在 PCL 中:

public interface IDialogProvider
{
void ShowMessage(string title, string message, string dismissButtonTitle, Action dismissed);
void Confirm(string title, string message, string okButtonTitle, string dismissButtonTitle, Action confirmed, Action dismissed);
}

======================================

在 Xamarin.iOS 中

public class TouchDialogProvider : IDialogProvider
{
public void ShowMessage(string title, string message, string dismissButtonTitle, Action dismissed)
{
var alert = UIAlertController.Create(title, message, UIAlertControllerStyle.Alert);
alert.AddAction(UIAlertAction.Create(dismissButtonTitle, UIAlertActionStyle.Default,
action => { if (null != dismissed) { dismissed.Invoke(); } }));
}

public void Confirm(string title, string message, string okButtonTitle, string dismissButtonTitle, Action confirmed, Action dismissed)
{
var alertView = new UIAlertView(title, message, null, dismissButtonTitle, okButtonTitle);
alertView.Clicked += (object sender, UIButtonEventArgs e) => {
if (e.ButtonIndex == 1 & null != confirmed)
{ confirmed.Invoke(); }
if (e.ButtonIndex == 0 & null != dismissed)
{ dismissed.Invoke(); } };
alertView.Show();
}
}

最佳答案

您永远不会在 ShowMessage 方法中显示 View Controller :

public void ShowMessage(string title, string message, string dismissButtonTitle, Action dismissed)
{
var alert = UIAlertController.Create(title, message, UIAlertControllerStyle.Alert);
alert.AddAction(UIAlertAction.Create(dismissButtonTitle, UIAlertActionStyle.Default,
action => { if (null != dismissed) { dismissed.Invoke(); } }));
var rootVC = UIApplication.SharedApplication.Windows[0].RootViewController;
rootVC.PresentViewController(alert, true, null);
}

另一个 Confirm 方法应该从 UIAlertView 更改为 UIAlertController,因为 UIAlertView 自 iOS 9 以来已被弃用。

关于ios - UIAlertView 不工作是 Xamarin.iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46364877/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com