2013-05-21 39 views
9

如何在Xamarin.Android中顯示消息框?我如何從消息框中得到是或否的答覆?如何在Monodroid中顯示MessageBox

---更新時間:

myBtn.Click += (sender, e) => 
{ 
    new AlertDialog.Builder(this) 
    .SetMessage("hi") 
    .Show(); 
}; 

回答

17

可以使用AlertDialog.Buider類從Activity之內。

new AlertDialog.Builder(this) 
    .SetPositiveButton("Yes", (sender, args) => 
    { 
     // User pressed yes 
    }) 
    .SetNegativeButton("No", (sender, args) => 
    { 
     // User pressed no 
    }) 
    .SetMessage("An error happened!") 
    .SetTitle("Error") 
    .Show(); 
+0

感謝。有用。但是,如何讓它看起來更像Windows phone中的MessageBox? MessageBox具有高度,寬度和背景顏色。 – MilkBottle

+1

請參閱xml從[this](http://stackoverflow.com/a/13763132/1411687)回答並使用[this](http://stackoverflow.com/a/3119581/1411687)的構造函數。請注意,在API級別11之前,由於較新的構造函數不可用,因此必須使用ContextThemeWrapper來設置對話框的主題。 –

2

試試這個:

public void ShowAlert (string str) 
     { 
      AlertDialog.Builder alert = new AlertDialog.Builder (this); 
      alert.SetTitle (str); 
      alert.SetPositiveButton ("OK", (senderAlert, args) => { 
       // write your own set of instructions 
       }); 

      //run the alert in UI thread to display in the screen 
      RunOnUiThread (() => { 
       alert.Show(); 
      }); 
     }