How can I pass a list between xamarin.forms pages?

I have a list of objects that need to be passed to another view, but I don't know how to do this in Xamarin.forms, I think I should be using setBinding, but that's the only thing I say in this case, Thanks.

List<Localizacao> localizacaoList = new List<Localizacao>(); ;
        if (localizacao != null && lojaPerto != null)
        {
            localizacaoList = new List<Localizacao>();
            Localizacao loc = new Localizacao();
            loc.latitude = Double.Parse(lojaPerto.latitude);
            loc.longitude = Double.Parse(lojaPerto.longitude);
            localizacaoList.Add(loc);
            localizacaoList.Add(localizacao);
        }

        var secondPage = new Views.ComoChegarView ();
        secondPage.BindingContext = localizacaoList;
        await Navigation.PushAsync(secondPage);

      

Actually, I posted, but I can't get it again in another view

+3


source to share


2 answers


If you are not using any additional structure, perhaps you can try using constructor parameters.

public partial class ComoChegarView
{
   ...
   private List<Localizacao> Locals{get;set;}       

   public ComoChegarView(List<Localizacao> locals)
   {
      InitializeComponent(); //standard code that mix xaml and code behind
      this.Locals = locals; //store the data in property
      this.BindingContext = this; //Set the binding context
   }

}

      

This way you can pass the value when creating the page.



List<Localizacao> localizacaoList = new List<Localizacao>(); ;
if (localizacao != null && lojaPerto != null)
{
    localizacaoList = new List<Localizacao>();
    Localizacao loc = new Localizacao();
    loc.latitude = Double.Parse(lojaPerto.latitude);
    loc.longitude = Double.Parse(lojaPerto.longitude);
    localizacaoList.Add(loc);
    localizacaoList.Add(localizacao);
}

var secondPage = new Views.ComoChegarView (localizacaoList);
await Navigation.PushAsync(secondPage);

      

Remember to update your binding in XAML to reflect property access (for example)

<ListView ItemsSource="{Binding Locals}">...</ListView>

      

0


source


What you want to achieve is perfectly supported by all serious MVVM libraries. 1) the view does not convey anything to another view, this is the job of ViewModels 2) in the context of MVVM you can use many methods to send ou pass data from one ViewModel to another, the main one being: Messenger MVVM (one of them is included in Xamarin.Forms) or dependency injection in the ViewModel's constructor (using an IoC container that most MVVM libraries offer using Unity, DryIoc, ...). This is definitely difficult to do if you don't master the MVVM pattern, but you should spend a little time learning this pattern and some libraries like Prism. You will quickly see the benefits of this approach and be very happy to write code more efficiently (and soon find quick and clean solutions to problems likewhich you are talking about here).



0


source







All Articles