2016-05-25 71 views
1

我想在我的Xamarin Android應用程序(C#)中顯示對話框警報,並且當我單擊按鈕時,我想在對話框中執行一些操作。在Android中設置對話框按鈕clicklistener Xamarin

從之前,我用這個代碼:

AlertDialog.Builder builder = new AlertDialog.Builder(this) 
    .SetTitle("Delete") 
    .SetMessage("Are you sure you want to delete?) 
    .SetPositiveButton("No", (senderAlert, args) => { }) 
    .SetNegativeButton("Yes", (senderAlert, args) => { 
    DatabaseHelper.Delete(item); 
}); 
builder.Create().Show(); 

爲了使隨便舉個例子,可以說我想保持對話框打開,直到該項目被刪除,但我想禁用是按鈕和在Android工作時更改消息文本。這可能來自我必須訪問對話框並更改它的代碼嗎? senderAlert和args都沒有任何有用的屬性或方法。

我一直在尋找其他的方法來建立我對話,我已經看到了這兩個:

1)This guy使用波紋管的方式,但我DialogInterface沒有.OnClickListener()

builder.setPositiveButton("Test", 
new DialogInterface.OnClickListener() 
{ 
    @Override 
    public void onClick(DialogInterface dialog, int which) 
    { 
     //Do stuff to dialog 
    } 
}); 

2)This guy正在使用IDialogInterfaceOnClickListener,我一直在試圖找到如何做到這一點的例子,但我還沒有找到任何。好像他正在使用null而不是我想要的代碼。

.setPositiveButton("OK", (Android.Content.IDialogInterfaceOnClickListener)null) 

任何想法?

回答

2

我用的是這樣的:

 using (var builder = new AlertDialog.Builder(Activity)) 
     { 
     var title = "Please edit your details:"; 
     builder.SetTitle(title); 
     builder.SetPositiveButton("OK", OkAction); 
     builder.SetNegativeButton("Cancel", CancelAction); 
     var myCustomDialog = builder.Create(); 

     myCustomDialog.Show(); 
     } 

     private void OkAction(object sender, DialogClickEventArgs e) 
     { 
     var myButton = sender as Button; //this will give you the OK button on the dialog but you're already in here so you don't really need it - just perform the action you want to do directly unless I'm missing something.. 
     if(myButton != null) 
     { 
      //do something on ok selected 
     } 
     } 
     private void CancelAction(object sender, DialogClickEventArgs e) 
     { 
     //do something on cancel selected 
     } 

例如:https://wordpress.com/read/feeds/35388914/posts/1024259222

+0

這看起來像我現在使用的幾乎相同的方式。我的問題是我寫「發件人」時只能訪問「Equals」,「GetHashCode」,「GetType」和「ToString」。在VIsual工作室。我需要施放它還是什麼? PS:無法訪問鏈接,需要登錄。 – mathkid91

+0

添加鑄造按鈕,例如... – Milen

+0

謝謝! :) var dialog = sender as AlertDialog;正是我想要的:) – mathkid91

1

你可以使用這個類

using Android.App; 
using System.Threading.Tasks; 

public class Show_Dialog 
{ 
    public enum MessageResult 
    { 
     NONE = 0, 
     OK = 1, 
     CANCEL = 2, 
     ABORT = 3, 
     RETRY = 4, 
     IGNORE = 5, 
     YES = 6, 
     NO = 7 
    } 

    Activity mcontext; 
    public Show_Dialog(Activity activity) : base() 
    { 
     this.mcontext = activity; 
    } 


    /// <summary> 
    /// Messbox function to show a massage box 
    /// </summary> 
    /// <param name="Title">to show Title for your messagebox</param> 
    /// <param name="Message">to show Message for your messagebox</param> 
    /// <param name="result">to get result for your messagebox; OK=1, Cancel=2, ingnore=3, else=0</param> 
    /// <param name="SetInverseBackgroundForced">to Set Inverse Background Forced</param> 
    /// <param name="SetCancelable">to set force message box is cancelabel or no</param> 
    /// <param name="PositiveButton">to show Title for PositiveButton</param> 
    /// <param name="NegativeButton">to show Title for NegativeButton</param> 
    /// <param name="NeutralButton">to show Title for your NeutralButton</param> 
    /// <param name="IconAttribute">to show icon for your messagebox</param> 
    /// <returns></returns> 
    public Task<MessageResult> ShowDialog(string Title, string Message, bool SetCancelable = false, bool SetInverseBackgroundForced = false, MessageResult PositiveButton = MessageResult.OK, MessageResult NegativeButton = MessageResult.NONE, MessageResult NeutralButton = MessageResult.NONE, int IconAttribute = Android.Resource.Attribute.AlertDialogIcon) 
    { 
     var tcs = new TaskCompletionSource<MessageResult>(); 

     var builder = new AlertDialog.Builder(mcontext); 
     builder.SetIconAttribute(IconAttribute); 
     builder.SetTitle(Title); 
     builder.SetMessage(Message); 
     builder.SetInverseBackgroundForced(SetInverseBackgroundForced); 
     builder.SetCancelable(SetCancelable); 

     builder.SetPositiveButton((PositiveButton != MessageResult.NONE) ? PositiveButton.ToString() : string.Empty, (senderAlert, args) => 
      { 
       tcs.SetResult(PositiveButton); 
      }); 
     builder.SetNegativeButton((NegativeButton != MessageResult.NONE) ? NegativeButton.ToString() : string.Empty, delegate 
     { 
      tcs.SetResult(NegativeButton); 
     }); 
     builder.SetNeutralButton((NeutralButton != MessageResult.NONE) ? NeutralButton.ToString() : string.Empty, delegate 
     { 
      tcs.SetResult(NeutralButton); 
     }); 

     Xamarin.Forms.Device.BeginInvokeOnMainThread(() => 
     { 
     }); 

     Xamarin.Forms.Device.BeginInvokeOnMainThread(() => 
     { 
      builder.Show(); 
     }); 


     // builder.Show(); 
     return tcs.Task; 
    } 
} 

,你可以使用異步或同步機能的研究

private void a() 
{ 
    Show_Dialog msg = new Show_Dialog(this); 
    msg.ShowDialog("Error", "Message"); 
} 

private async void b() 
{ 
    Show_Dialog msg1 = new Show_Dialog(this); 
    if (await msg1.ShowDialog("Error", "Message", true, false, Show_Dialog.MessageResult.YES, Show_Dialog.MessageResult.NO) == Show_Dialog.MessageResult.YES) 
    { 
     //do anything 
    } 
} 
相關問題