2016-01-20 96 views
0

我正在使用Xamarin和Nuget的V7支持庫。當按下中立按鈕時阻止Android AlertDialog關閉

我想覆蓋默認的Dialog.Dismiss()行爲。我已經沒有運氣嘗試這樣做,在對話框關閉時,中性(刷新)按鈕:

爲清楚起見

更新:我需要的行爲是被按壓中性按鈕後MyDialog保持打開狀態。目前,當按下中性按鈕時,refreshMyDialogButton_Click方法觸發,然後MyDialog被自動解除。

using AlertDialog = Android.Support.V7.App.AlertDialog; 

.... 
protected AlertDialog MyDialog; 

protected override void OnCreate(Bundle savedInstanceState) 
{ 
    base.OnCreate(savedInstanceState); 
    var myDialogView = View.Inflate(this, Resource.Layout.myDialogView, null); 
    MyDialog = createMyDialog(myDialogView); 
    MyDialog.Show(); 
} 

protected AlertDialog createMyDialog(View myDialogView) 
{ 
    var builder = new AlertDialog.Builder(this); 
    builder 
     .SetNegativeButton("back", delegate { MyDialog.Dismiss(); }) 
     .SetNeutralButton("refresh", (EventHandler<DialogClickEventArgs>)refreshMyDialogButton_Click) 
     .SetPositiveButton(/**foo**/) 
     .SetView(/**my custom view**/) 
     .SetTitle(/**title**/) 
     .SetMessage(/**message**/) 
     ; 
    return builder.Create(); 
} 

private void refreshMyDialogButton_Click(object sender, EventArgs e) 
{ 
    /** start async tasks but don't close dialog **/ 
} 

更新2

添加以下代碼到onCreate方法,但是這導致一個空引用異常上dialogNeutral

protected override void OnCreate(Bundle savedInstanceState) 
{ 
    base.OnCreate(savedInstanceState); 
    var myDialogView = View.Inflate(this, Resource.Layout.myDialogView, null); 
    MyDialog = createGathererDialog(myDialogView); 
    var dialogNeutral = MyDialog.GetButton((int)DialogButtonType.Neutral); 
    dialogNeutral.Click += refreshMyDialogButton_Click; 
} 

protected AlertDialog createMyDialog(View myDialogView) 
{ 
    var builder = new AlertDialog.Builder(this); 
    builder 
     .SetNegativeButton("back", delegate { MyDialog.Dismiss(); }) 
     .SetNeutralButton("refresh", (EventHandler<DialogClickEventArgs>)null) 
     .SetPositiveButton(/**foo**/) 
     .SetView(/**my custom view**/) 
     .SetTitle(/**title**/) 
     .SetMessage(/**message**/) 
     ; 
    return builder.Create(); 
} 
+0

有關使用setCancelable(假的)什麼? –

+0

澄清 - 您的問題是對話關閉的事實,然後觸發事件或關閉它,而不會觸發它? – EvilBeer

+0

@EvilBeer感謝您的回覆,我已經更新了我的問題以獲得清晰度 – tallpaul

回答

0

該按鈕必須被實例化爲null然後在.Show()方法被調用後通過.GetButton()進行訪問。

0

我知道我遲到了這個答案,但你可以在你的對話課上試試這個。

this.Dialog.SetCanceledOnTouchOutside(false); 

,並同時從外部訪問,

dialogBox.Dialog.SetCanceledOnTouchOutside(false); 
相關問題