2013-05-21 25 views
9

Xamarin.Androidにメッセージボックスを表示するにはどうすればよいですか?メッセージボックスからはいまたはいいえ応答を取得するにはどうすればよいですか?MonodroidでMessageBoxを表示する方法

は---更新:

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

答えて

17

あなたはActivity内からAlertDialog.Buiderクラスを使用することができます。

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

[this](http://stackoverflow.com/a/13763132/1411687)の回答からxmlを参照し、[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(); 
      }); 
     } 
関連する問題