如何在Monodroid中显示MessageBox

时间:2013-05-21 01:14:20

标签: c# android xamarin.android android-alertdialog

如何在Xamarin.Android中显示消息框?如何从消息框中获得是或否的响应?

---更新:

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

2 个答案:

答案 0 :(得分:20)

您可以在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();

答案 1 :(得分: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 ();
            });
        }