在进度对话框上添加取消按钮

Add cancel button on progress dialog

我试图在 Xamarin Android 中启用 ProgressDialog 上的取消按钮,但它没有出现。

这是我到目前为止所做的:

ProgressDialog progressDialog = new ProgressDialog(Context);
progressDialog.SetProgressStyle(ProgressDialogStyle.Horizontal);
progressDialog.SetCancelable(true);

progressDialog.CancelEvent += (o, e) =>
{
    // Cancel download
};

progressDialog.Show();

相关问题:How to set cancel button in Progress Dialog? or Android ProgressDialog can't add Cancel button

我设法通过以下方式做到了:

progressDialog.SetButton("Cancel", new EventHandler<DialogClickEventArgs>(
    (s, args) => { 
        // Cancel download
    }
));

注意:ProgressDialog 现在 deprecated 在 API-26

var progress = new ProgressDialog(this);
progress.SetTitle("Syncing Events");
progress.Indeterminate = false;
progress.SetProgressStyle(ProgressDialogStyle.Horizontal);
progress.Max = totalEvents;
progress.Progress = currentEvent;
progress.SetButton(-3, "CancelLeft", (sender, e) => {
    Log.Debug("SO", "Cancel");
});
progress.SetButton(-2, "CancelMiddle", (sender, e) =>
{
    Log.Debug("SO", "Cancel");
});
progress.SetButton(-1, "CancelRight", (sender, e) =>
{
    Log.Debug("SO", "Cancel");
});
progress.Show();

ProgressDialog myDialog = new ProgressDialog(YourActivity.this);
myDialog.setMessage("Loading...");
myDialog.setCancelable(false);
myDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        dialog.dismiss();
    }
});
myDialog.show();