0

I am developing a contact book application wherein I have a listview with a add button. On clicking the add button it redirects the user to a modal page asking for it's name, email id, institution. All of this details are saved in a ContactDetails class. On clicking done button on the modal page I want to display the contents in my listview.

Any suggestions on how to implement this successfully.

Code am currently working on are as follows: ContactDetails.cs

public class ContactDetails
{
    public string Name { get; set; }
    public string Inst { get; set; }
    public string EmailId { get; set;}  
    public int Mob { get; set;}
}

ContactDetailsModalPage.xaml.cs

public partial class ContactDetailsModalPage: ContentPage
{
    CandidateDetails cd = new CandidateDetails();
    async void OnDoneClicked(object sender, System.EventArgs e)
    {
        cd.Name = (string)candNameEntry.Text; 
        cd.Inst = (string)candInst.Text;
        cd.EmailId = (string)candEmailId.Text;
        cd.Mob = Convert.ToInt32(candMobNumber.Text);

        List<ContactDetails> candList = new List<ContactDetails>();
        candList.Add(cd);

        await Navigation.PopModalAsync();

    }


    public CandidateDetailsModalPage()
    {
        InitializeComponent();

    }
}

I am not able to understand as to what should I provide as the itemsource for my listview so that it can dynamically display the value that are stored in the ContactDetails class by modal page.

1
  • In Xamarin Forms, any property i.e candList assigned to [ListView] will automatically listen to the changes and will update the view! Commented Feb 27, 2017 at 16:59

2 Answers 2

1

You need to use an ObservableCollection instead of a List. An ObservableCollection will notify any data bound element of changes.

Sign up to request clarification or add additional context in comments.

Comments

1

I'm afraid you're giving us too little information.

  1. Are you using the MVVM pattern? If not: You need a reference to the ListView and add a new Cell. If yes: Please post your ViewModel holding the bound ContactDetails List.
  2. Like @Jason said, if there is a List of ContactDetails that is bound to the ItemsSourceof the ListView - it needs to be an ObservableCollection<ContactDetails>. All you have do do in your ClickHandler is to get a reference to that List and add / insert the new item.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.