How to access a list of another class in C#

Asked

Viewed 866 times

2

I’m trying to access the Contact Contact List list in the Contacts List class to view. The problem is that although the list is filled in Contact, in Contacts Lists it appears empty.

    public class Contato
        {
            string Nome { get; set; }
            string Numero {get; set; }

            public List<Contato> ListaDeContatos = new List<Contato>();
            public async void Buscar()
            {
                 ContactStore contactStore;
                 contactStore = await ContactManager.RequestStoreAsync();
                 var contatos = await contactStore.FindContactsAsync();

                 foreach (var item in contatos)
                 {
                      if (!string.IsNullOrEmpty(item.FirstName))
                      {                                      
                           ListaDeContatos.Add(new Contato() { Nome = item.FirstName.ToString() + " " + item.LastName.ToString(), Numero = "12345678" });
                 }
            }
    }

        public sealed partial class ListaContatos : Page
    {       
        public ListaContatos()
        {
...         
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            Contato contato = new Contato();
            contato.Buscar();
            listView.ItemsSource = contato.ListaDeContatos;
        }

1 answer

1

Your Find() method has no Return and you are using a static variable within the Find() method, so you will not be able to fill your List object.

Create the search method and return the filled list:

 public async ListaDeContatos Buscar()
 {
   return ListaDeContatos;
 }

Browser other questions tagged

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