我的Xamarin.native项目针对Android& iOS版。我无法在Xamarin.iOS中显示警报;以下代码适用于Xamarin.Android。
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);
}
======================================
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();
}
}
答案 0 :(得分:3)
您永远不会在ShowMessage
方法中展示视图控制器:
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
,因为自iOS 9以来已弃用UIAlertView。
答案 1 :(得分:1)
请尝试使用以下一项来创建警报,我始终仅使用此一项。
void CreateAlert(string title, string buttonTitle, RelayCommand buttonCommand, string error, RelayCommand closeButtonCommand = null)
{
var elements = new List<InfoDialogElement>{
new InfoDialogElement
{
Type = InfoDialogElementTypeEnum.Text,
MarginTop = 25,
MarginBottom = 25,
SideMargin = 0,
Info = error,
TextSize = 16,
Font = FontStyleEnum.OoredooHeavy
}
};
var dialog = new OoInfoDialog(title, buttonTitle, buttonCommand, elements);
if (closeButtonCommand != null)
dialog.CloseCommand = closeButtonCommand;
if (DialogBeingPresented != null)
DialogBeingPresented.PresentDialog(dialog, UIColor.FromRGBA(0, 0, 0, 230));
else
{
var parentViewController = AppDelegate.Current.Window.RootViewController;
if (parentViewController != null)
{
if (parentViewController is UINavigationController)
{
(parentViewController as UINavigationController).TopViewController.PresentDialog(dialog, UIColor.FromRGBA(0, 0, 0, 230));
} else
{
parentViewController.PresentDialog(dialog, UIColor.FromRGBA(0, 0, 0, 230));
}
}
}
}
答案 2 :(得分:0)
将您的AlertView显示为PresentViewController
InvokeOnMainThread(() =>
{
var alert = UIAlertController.Create(title, message, UIAlertControllerStyle.Alert);
alert.AddAction(UIAlertAction.Create(dismissButtonTitle, UIAlertActionStyle.Default, action => { if (null != dismissed) { dismissed.Invoke(); } }));
PresentViewController(alert, true, null);
});
答案 3 :(得分:0)
我认为有一种更简单的方法来制作警报弹出窗口。你可以这样试试:
UIAlertView alert = new UIAlertView()
{
Title = "Network error",
Message = "Please check your internert connection"
};
alert.AddButton("Cancel");
alert.AddButton("OK");
alert.Show();
alert.Clicked += Alert_Clicked;
void Alert_Clicked(object sender, UIButtonEventArgs e)
{
if(e.ButtonIndex == 1 )
{
//your action on alert dialog OK button click
}
}
答案 4 :(得分:0)
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(); } }));
//UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(alert, true, null);
UIAlertController actionSheetAlert = UIAlertController.Create("App!!!", "\n" +
" " +
"1. User Created", UIAlertControllerStyle.Alert);
actionSheetAlert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, action => { if (null != dismissed) { dismissed.Invoke(); } }));
UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(actionSheetAlert, true, null);
}
这是我的工作代码
这样称呼
ShowMessage("s", "s", "s", null);
“ s”只是伪文本,一切都在ShowMessage()中完成