[英]Xamarin Forms Display Alert Disappear
i'm developing an app in Xamarin Forms, but i've noticed a strange behavior, when i display an alert, in Android (version major than 4.2.2), if i press outside the alert modal, the alert disappear immediately. 我正在开发一个Xamarin Forms的应用程序,但是我注意到一个奇怪的行为,当我在Android(版本大于4.2.2)中显示警报时,如果我按下警报模式,警报会立即消失。 There is any way to prevent this? 有什么方法可以防止这种情况吗? I want that the alert disappear only on user selection. 我希望警报仅在用户选择时消失。
Many thanks 非常感谢
When a dialog is shown in Android, clicking just outside of it to cancel is pretty standard. 当在Android中显示对话框时,单击其外部取消是非常标准的。 To change this behavior you will need to create an interface in your PCL: 要更改此行为,您需要在PCL中创建一个界面:
public interface ICustomAlert
{
void ShowAlert(string message);
}
In your Android project create the implementation (something like this): 在你的Android项目中创建实现(类似这样):
[assembly: Xamarin.Forms.Dependency (typeof (AndroidCustomAlert))]
public class AndroidCustomAlert : ICustomAlert
{
void ShowAlert(string message)
{
var builder = new AlertDialog.Builder(Xamarin.Forms.Forms.Context);
builder.SetMessage(message);
builder.SetPositiveButton("OK", (sender, args) => { });
builder.SetCancelable(false);
builder.Show();
}
}
Notice the SetCancelable(false)
. 注意SetCancelable(false)
。 That's what makes it so the user can't click outside of the alert and make it disappear. 这就是使得用户无法点击警报之外并使其消失的原因。
To use it, get ICustomAlert from the dependency service and call ShowAlert: 要使用它,请从依赖服务获取ICustomAlert并调用ShowAlert:
DependencyService.Get<ICustomAlert>().ShowAlert("Hello!");
Implement your own DisplayAlert with a PopupLayout. 使用PopupLayout实现自己的DisplayAlert。
OR 要么
Make use of ACR User Dialogs Plugin for Xamarin by Allan Ritchie 使用Allan Ritchie的 Xamarin ACR用户对话插件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.