I am looking for a solution, to have a list view with items in the list, that is working, and I would like to make it so if I click on an item on the list, I would get the detailed view of the item.
So my problem is, to how to bind the detail view to the selected item. this is where my data comes for now
public class Friend
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public int PhoneNumber { get; set; }
public string Email { get; set; }
public Uri Picture { get; set; }
public string GPS { get; set; }
public Uri URL { get; set; }
public override string ToString()
{
return Name + " Address: " + Address + " Phone Number: " + PhoneNumber;
}
//firend data
public static List<Friend> GetList()
{
var friends = new List<Friend>();
friends.Add(new Friend { Id = 1, Name = "t1", Email = "t1@t1.com", Address = "xx", GPS = "22t,e33", PhoneNumber = 523254854, Picture = new Uri(""), URL = new Uri("") });
friends.Add(new Friend { Id = 2, Name = "t2", Email = "t1@t1.com", Address = "xx", GPS = "22t,e35", PhoneNumber = 222222, Picture = new Uri(""), URL = new Uri("") });
friends.Add(new Friend { Id = 3, Name = "t3", Email = "t1@t1.com", Address = "xx", GPS = "22t,e38", PhoneNumber = 111111, Picture = new Uri(""), URL = new Uri("") });
return friends;
}
}
this is my mainview public partial class MainPage : ContentPage {
public MainPage()
{
InitializeComponent();
MainListView.ItemsSource = Friend.GetList();
}
private async void MainListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
if (e.SelectedItem != null)
{
var PhotoPage = new Page1(e.SelectedItem);
await Navigation.PushAsync(PhotoPage);
((ListView)sender).SelectedItem = null; // de-select the row
}
}
}
Thanks link to project on git
Jason is correct there your code is correct, but maybe this can clear your mind:
private async void MainListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
if (e.SelectedItem != null)
{
Friend friend = e.SelectedItem as Friend;
await Navigation.PushAsync(new FriendPage(friend));
((ListView)sender).SelectedItem = null;
}
}
//Where
//FriendPage:
public class FriendPage : ContentPage
{
public FriendPage(Friend friend)
{
//... friend parameter will have the selected list item.
}
}