xamarin android异步任务警报对话框

smdnsysy  于 2023-01-18  发布在  Android
关注(0)|答案(1)|浏览(144)

我使用Visual Studio 2015和xamarin Android我有一个小项目,其中包含一个按钮和一个textview单击按钮时,我启动一个for(int=0...)方法,尝试获取某个网页的内容,并将此内容放置到textview中。此方法是异步的
很好用。
现在,我想在每个循环(for)上添加一个警报对话框,并等待用户的响应?我的问题是,当我启动调试器时,方法for(..)开始下载每个循环的文件,并显示每个循环的警报对话框...而不等待响应!
我是新来的任务,我敢肯定我不知道如何使用或其他东西。任何想法,如何等待响应的警报对话框。
这里是它的代码:

private async void Button_Click(object sender, EventArgs e)
{
    FindViewById<TextView>(Resource.Id.textView1).Text = "";
    await CreateMultipleTasksAsync();
    FindViewById<TextView>(Resource.Id.textView1).Text += "Control returned to startButton_Click.";
}

下面是async方法的代码:

async Task CreateMultipleTasksAsync()
{
    HttpClient client = new HttpClient() { MaxResponseContentBufferSize = 1000000 };

    int max = 3;
    for (int i = 1; i <= max; i++)
    {
        // Create and start the tasks. As each task finishes, DisplayResults  
        // displays its length.
        Task<int> download1 = ProcessURLAsync("http://msdn.microsoft.com", client);
        Task<int> download2 = ProcessURLAsync("http://msdn.microsoft.com/en-us/library/hh156528(VS.110).aspx", client);
        Task<int> download3 = ProcessURLAsync("http://msdn.microsoft.com/en-us/library/67w7t67f.aspx", client);

        // Await each task. 
        int length1 = await download1;
        int length2 = await download2;
        int length3 = await download3;

        // Display the total count for the downloaded websites.
        int total = length1 + length2 + length3;
        FindViewById<TextView>(Resource.Id.textView1).Text += string.Format("\r\n\r\n Start {0} Tour for an total bytes returned:  {1}\r\n", i, total);

        //display alertDialog
        var x = await displayAlertDialogBox(i);
    }
}

最后一个代码是警报对话框

async Task<int> displayAlertDialogBox(int loop)
{
    int ret = 0;
    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    //
    AlertDialog dialog = builder.Create();
    dialog.SetTitle("ALERT DIALOG");
    dialog.SetMessage(string.Format("COUNT {0} ITEM {1}", loop, "content"));
    dialog.SetCancelable(true);
    dialog.SetButton("OK button", (z, ev) =>
    {
        Toast.MakeText(this, string.Format("Ok button {0}", loop), ToastLength.Long).Show();
        ret = 99;
    });
    dialog.SetButton2("Cancel button", (z, ev) =>
    {
        Toast.MakeText(this, string.Format("Cancel button {0}", loop), ToastLength.Long).Show();
        ret = 1;
    });

    //
    dialog.Window.SetGravity(GravityFlags.Center);
    dialog.Show();

    //
    return (ret > 0) ? 1 : 0;
}
7rtdyuoh

7rtdyuoh1#

此扩展方法允许异步显示Android.AlertDialog

public static async Task<bool?> ShowAlertDialogAsync(this Context context,
        string title, string message,
        string positive = default,
        string negative = default,
        string neutral = default)
    {
        var source = new TaskCompletionSource<bool?>();

        new AlertDialog.Builder(context).SetTitle(title).SetMessage(message)
            .SetPositiveButton(positive, (sender, args) => source.SetResult(true))
            .SetNegativeButton(negative, (sender, args) => source.SetResult(false))
            .SetNeutralButton(neutral, (sender, args) => source.SetResult(default))
            .Show();

        return await source.Task;
    }

如有疑问

var result = await activity.ShowAlertDialogAsync
    (
        "Question", "Confirm the action?", "Yes", "No"
    );
    
    if (result is true) PerformAction();

通知

await activity.ShowAlertDialogAsync
    (
        "Notice", "Hey, you are so fine!", neutral: "Ok"
    );

相关问题