Update Listview after return with Popasync in Xamarin Forms

Asked

Viewed 1,341 times

2

How to update the ListView after returning to the View using the PopAsync ? I need the ListView load items from it after the Address Insertion View saves a new address.

OBS: The PushAsync or PushModalAsync return to the View correctly, but wrong for what I need.

Inserirenderecoviewmodel:

    if (Api.APICliente.InserirEndereco(objEnd) == 0)
    {
         if (alteracao == false)
         {
             await Page.DisplayAlert("Mensagem", "Endereço inserido com sucesso!", "OK");
         }
         else
             await Page.DisplayAlert("Mensagem", "Endereço alterado com sucesso!", "OK");
    }
    else
        await Page.DisplayAlert("Alerta", "Não foi possível inserir os dados!", "OK");

// Aqui retorna para a View de MeusEndereços onde contem a ListView que irá carregar os endereços cadastrados.
await Page.Navigation.PopAsync(); 
  • By the title/formulation of the question you are already managing to pass the data, already getting it? It remains only to update the Listview? How’s the code of Listviewto be filled?

4 answers

1

Override the Onappearing method in your view.

For example:

 protected override void OnAppearing()
    {
        base.OnAppearing();
        lstParceiro.SelectedItem = null;
        AtualizarLista();
    }

In this case I have delegated to a method (Updater) the task of updating Itemssource from my list.

1


In your View of Myaddresses

protected override void OnAppearing()
{
    base.OnAppearing();
    BindingContext = new suaViewModel();
}

1

To fill a Listview in Xamarin.Forms it is always recommended to use an Observablecollection as it notifies any change to the collection(Insert,delete,update). Good with a Observablecolection in Listview’s Itemsource using the MVVM standard your Listview Page has an Event called Appearing You Can Call the method of your viewModel that fill in the listview data in this event. Example :

    public partial class Page : ContentPage
    {
        private PageViewModel vm;
        public Page()
        {
            InitializeComponent();
            vm = new PageViewModel(this.Navigation);
            this.BindingContext = vm;
            this.Appearing += TennisClubMessagePage_Appearing;
        }

        private void TennisClubMessagePage_Appearing(object sender, System.EventArgs e)
        {
            vm.LoadDataAsync();
        }
    }

0

Create a event handler in the viewmodel, and another in view, when calling form, whether modal or not.

In the form who will call the form modal, make the following call:

var modalform = new seumodalform();
modalform.seuviewhandler += async(O,e)=>{ o valr que voce quer atualizar = O;}

In the builder of view just define:

seuVM.seuVMHandler = seuViewHandler;

//E na rotina que fechar o view, se for realizado via mvvm
seuVMhandler?.invoke(objeto, args);

//Se for feito via codebehind da view, basta fazer o mesmo invoke
seuViewhandler?.invoke(objeto, args);

Browser other questions tagged

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