Update Page after Popasync in Xamarinforms

Asked

Viewed 370 times

0

I have a page with the expense list contains travel information like Description gross value, expenses and net total and even a list with my travel expenses. The problem is that when I will edit or create a new expense, and give the Popasync command to go back to Expense List, the updated expense list is loaded and also the updated trip values, but only Listview is updated and information passed by Bindcontext = trip is not updated. Does anyone have an idea how to update this information when returning to the previous page?

    Viagem _viagem;
    Movimento _movimento;
    ObservableCollection<Despesa> _listaDespesa;

    public ListaDespesaViagemPage (Viagem viagem, Movimento movimento)
    {
        InitializeComponent ();
        _viagem = viagem;
        _movimento = movimento;
    }

    private void carregarLista()
    {
        BindingContext = _viagem;
        DespesaDal despesaDal = new DespesaDal();
        _listaDespesa = new ObservableCollection<Despesa>(despesaDal.GetDespesasViagem(_viagem.ViagemId));
        ltvDespesas.ItemsSource = _listaDespesa;
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();
        carregarLista();
    }

[Resolution]

I used another approach that ended up solving. Instead of BindingContext = _viagem and in XAML have Text="{Binding Data}" I named the label x:Name="lblData" and at the event OnAppearing i passed the information to the specific label lblData.Text = _viagem.Data.ToString("dd/MM/yyyy").

New Code became:

C#

    private void carregarTela()
    {
        lblDescricao.Text = _viagem.Descricao;
        lblData.Text =  _viagem.Data.ToString("dd/MM/yyyy");
        lblValorBruto.Text = String.Format("{0:C2}", _viagem.Valor);
        lblDespesas.Text = String.Format("{0:C2}", _viagem.ValorDespesas);
        lblLiquido.Text = String.Format("{0:C2}", _viagem.Liquido);

        DespesaDal despesaDal = new DespesaDal();
        _listaDespesa = new ObservableCollection<Despesa>(despesaDal.GetDespesasViagem(_viagem.ViagemId));
        ltvDespesas.ItemsSource = _listaDespesa;
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();
        carregarTela();
    }

XAML

<Label x:Name="lblDescricao"/>
<Label x:Name="lblData"/>
<Label x:Name="lblValorBruto" />
<Label x:Name="lblDespesas"/>
<Label x:Name="lblLiquido"/>

Thank you all...

1 answer

0

I had this problem a few times, try setting Itemssource to null before assigning to the list:

private void carregarLista()
{
    BindingContext = _viagem;
    DespesaDal despesaDal = new DespesaDal();
    _listaDespesa = new ObservableCollection<Despesa>(despesaDal.GetDespesasViagem(_viagem.ViagemId));
    ltvDespesas.ItemsSource = null;
    ltvDespesas.ItemsSource = _listaDespesa;
}
  • Thank you for the answer. I will test this solution on another screen with the same problem, because in this solved using another approach...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.