Update ListView after return with PopAsync in Xamarin Forms

2

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

OBS: The PushAsync or PushModalAsync returns to View correctly, however wrong for what I need.

InsertEnderecoViewModel:

    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(); 
    
asked by anonymous 10.01.2017 / 19:30

4 answers

1

In your View of MyEndirects

protected override void OnAppearing()
{
    base.OnAppearing();
    BindingContext = new suaViewModel();
}
    
26.10.2017 / 22:17
1

To fill a ListView in Xamarin.Forms, it is always recommended to use an ObservableCollection because it notifies any collection changes (insert, delete, update). Good with an ObservableColection in the ItemSource of the ListView using the default MVVM your Page of the ListView has an Event called Appearing You can call the method of your viewModel that populates the data of the listview 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();
        }
    }
    
11.01.2017 / 01:09
1

Override the OnAppearing method of your view.

For example:

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

In this case I delegated to a method (UpdateList) the task of updating the ItemsSource of my list.

    
24.01.2017 / 14:40
0

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

In% with% to call form , make the following call:

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

In the constructor of form modal , 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);
    
07.08.2017 / 23:28