xamarin - xamarin.ios 在外部单击时不会关闭 UIAlertController
问题描述
我正在从事xamarin.forms
项目工作,但遇到了一个问题。
UIAlertController
我希望无论何时(以 android 术语Toast
)显示主屏幕都保持启用状态。对我来说,这两件事都很重要。
显示警报时,背景视图中的按钮应该是可点击的。并且由于需要显示重要消息,因此警报也应在给定时间内并行显示。
在 android 中,Toast
不会干扰主屏幕上的用户交互。我可以在 iOS 中有同样的工作吗?
这是我在依赖服务中尝试过的。
void ShowAlert(string message, double seconds)
{
try
{
if (alert == null && alertDelay == null)
{
alertDelay = NSTimer.CreateScheduledTimer(seconds, (obj) =>
{
Device.BeginInvokeOnMainThread(() =>
{
DismissMessage();
});
});
Device.BeginInvokeOnMainThread(() =>
{
try
{
alert = UIAlertController.Create("", message, UIAlertControllerStyle.Alert);
alert.View.UserInteractionEnabled = true;
topViewControllerWithRootViewController(UIApplication.SharedApplication.KeyWindow.RootViewController).PresentViewController(alert, true, () =>
{
UITapGestureRecognizer tap = new UITapGestureRecognizer(() => { }); // I have tried this but nothing happens
alert.View.Superview.Subviews[0].AddGestureRecognizer(tap);
});
}
catch (Exception ex)
{
var Error = ex.Message;
}
});
}
}
catch (Exception ex)
{
var Error = ex.Message;
}
}
void DismissMessage()
{
if (alert != null)
{
alert.DismissViewController(true, null);
alert = null;
}
if (alertDelay != null)
{
alertDelay.Dispose();
alertDelay = null;
}
}
UIViewController topViewControllerWithRootViewController(UIViewController rootViewController)
{
try
{
if (rootViewController is UITabBarController)
{
UITabBarController tabBarController = (UITabBarController)rootViewController;
return topViewControllerWithRootViewController(tabBarController.SelectedViewController);
}
else if (rootViewController is UINavigationController)
{
UINavigationController navigationController = (UINavigationController)rootViewController;
return topViewControllerWithRootViewController(navigationController.VisibleViewController);
}
else if (rootViewController.PresentedViewController != null)
{
UIViewController presentedViewController = rootViewController.PresentedViewController;
return topViewControllerWithRootViewController(presentedViewController);
}
}
catch (Exception)
{
}
return rootViewController;
}
解决方案
没有 iOS 原生的等价物。但是,这里有很多方法可以做到这一点:Toast equivalent for Xamarin Forms
那里提到的众多解决方案之一将起作用,如果您正在寻找本机解决方案,您可以显示一个警报,该警报会在指定时间后自行关闭,如下所示:
public class MessageIOS
{
const double LONG_DELAY = 3.5;
const double SHORT_DELAY = 2.0;
NSTimer alertDelay;
UIAlertController alert;
public void LongAlert(string message)
{
ShowAlert(message, LONG_DELAY);
}
public void ShortAlert(string message)
{
ShowAlert(message, SHORT_DELAY);
}
void ShowAlert(string message, double seconds)
{
alertDelay = NSTimer.CreateScheduledTimer(seconds, (obj) =>
{
dismissMessage();
});
alert = UIAlertController.Create(null, message, UIAlertControllerStyle.Alert);
UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(alert, true, null);
}
void dismissMessage()
{
if (alert != null)
{
alert.DismissViewController(true, null);
}
if (alertDelay != null)
{
alertDelay.Dispose();
}
}
}
推荐阅读
- laravel - [Route: messenger.store] 缺少必需的参数
- c# - 在 Visual Studio 2017 中运行和调试 Powershell 模块
- python - 张量流无效符号
- python-3.x - 使用 CSS 选择器在 BeautifulSoup find_all 方法中返回的空对象
- ui-grid - 数据刷新后的角度 ui-grid 过滤
- .net - Appsettings.json 不同的版本?
- javascript - 如何检查用户是否在 AWS Amplify 中被确认?
- javascript - Bot 框架范围 beginDialogAction
- html - 覆盖 * scss 页面中的 CSS 属性
- reactjs - 实施redux-persist后反应本机应用程序保持空白?