UIAlertView弃用了Xamarin iOS

时间:2017-09-06 21:38:50

标签: xamarin xamarin.ios

我正在尝试将以下代码更改为UIAlertController,但我收到一条错误消息“非可调用成员'UIAlertController'不能像方法一样使用”

这是AppDelegate.cs中的原始代码

private void remoteNotificationPayload(NSDictionary userInfo)
    {
        var aps_d = userInfo["aps"] as NSDictionary;
        var alert_d = aps_d["alert"] as NSDictionary;
        var body = alert_d["body"] as NSString;
        var title = alert_d["title"] as NSString;

        this.showAlert(title, body);
    }

private void showAlert(string title, string message)
{
  var alert = new UIAlertView(title ?? "Title", message ?? "Message", null, "Cancel", "OK");
  alert.Show();
}

我试过这就是我收到错误的地方:

private void showAlert(string title, string message)
{
 var okCancelAlertController = UIAlertController(title ?? "Title", message ?? "Message", preferredStyle: UIAlertControllerStyle.Alert);

 okCancelAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, alert => Console.WriteLine("Okay was clicked")));

 okCancelAlertController.AddAction(UIAlertAction.Create("Cancel", UIAlertActionStyle.Cancel, alert => Console.WriteLine("Cancel was clicked")));
UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(okCancelAlertController, true, null);
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:4)

在UIAlertController上使用静态Create方法:

var okCancelAlertController = UIAlertController.Create(title, message, UIAlertControllerStyle.Alert);