[英]Xamarin Forms Display Alert Disappear
我正在開發一個Xamarin Forms的應用程序,但是我注意到一個奇怪的行為,當我在Android(版本大於4.2.2)中顯示警報時,如果我按下警報模式,警報會立即消失。 有什么方法可以防止這種情況嗎? 我希望警報僅在用戶選擇時消失。
非常感謝
當在Android中顯示對話框時,單擊其外部取消是非常標准的。 要更改此行為,您需要在PCL中創建一個界面:
public interface ICustomAlert
{
void ShowAlert(string message);
}
在你的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();
}
}
注意SetCancelable(false)
。 這就是使得用戶無法點擊警報之外並使其消失的原因。
要使用它,請從依賴服務獲取ICustomAlert並調用ShowAlert:
DependencyService.Get<ICustomAlert>().ShowAlert("Hello!");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.