Wait with AlertDialog ()

0

Good! My problem is with AlertDialog asynchronous Xamarin.Android . I have a class with a method that I set an alert with some fields ... With the data from these fields, I mount an object to make my CRUD . The problem is when it is time to call AlertDialog , when I call, it is mounted and then instantiate my object with the empty values of AlertDialog , because it is asynchronous.

llCompraAtiva.Click += delegate
{
    this.RunOnUiThread(() => dadosCompra = Util.modalPagamento(this));
    string teste = dadosCompra.NumCartao;
};

I thought about putting something together with Thread , but I did not do well ...

In short: I need to enter the data in the inputs, then instantiate the object with the values of those inputs.

Can anyone help me? Tks

    
asked by anonymous 21.11.2016 / 20:20

1 answer

1

It is not recommended to block the Main Thread to wait for Dialog, so you need to provide the handler for the Dialog itself to handle, so when the user confirms the Dialog action, the Dialog will trigger the handler to fill the object .

I wrote an example, but I did not have time to test it, I think it will give you an idea

using (var builder = new AlertDialog.Builder(this))
{
    var title = "Edite seus detalhes: ";
    builder.SetTitle(title);
    builder.SetPositiveButton("OK", OkAction);                
    var customDialog = builder.Create();
    customDialog.Show();
}

And the button handler:

private void OkAction(object sender, DialogClickEventArgs e)
{
    var botao = sender as Button; 
    if (botao != null)
    {
        var resultado = BuscaResultado();
        _dadosCompra.NumeroCartao = resultado;
    }
}
    
22.11.2016 / 17:06