我正在尝试将以下代码更改为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);
}
感谢您的帮助。
答案 0 :(得分:4)
在UIAlertController上使用静态Create
方法:
var okCancelAlertController = UIAlertController.Create(title, message, UIAlertControllerStyle.Alert);