Listview 具有列表视图和详细视图的Xamarin表单应用程序
我正在寻找一个解决方案,拥有一个列表视图,其中包含列表中的项目,这是可行的,我希望这样,如果我单击列表中的某个项目,我将获得该项目的详细视图 因此,我的问题是,如何将详图视图绑定到所选项目。 这就是我目前的数据来源Listview 具有列表视图和详细视图的Xamarin表单应用程序,listview,xamarin,xamarin.forms,bind,xamarin-studio,Listview,Xamarin,Xamarin.forms,Bind,Xamarin Studio,我正在寻找一个解决方案,拥有一个列表视图,其中包含列表中的项目,这是可行的,我希望这样,如果我单击列表中的某个项目,我将获得该项目的详细视图 因此,我的问题是,如何将详图视图绑定到所选项目。 这就是我目前的数据来源 public class Friend { public int Id { get; set; } public string Name { get; set; } public string Address { get; set; } public
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;
}
}
谢谢
Jason是正确的您的代码是正确的,但这可能会让您头脑清醒:
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.
}
}
有什么问题?你所做的通常是正确的方法。在将e.SelectedItem传递到第1页之前,您可能想尝试将其强制转换为Friend,但我看不出有任何问题。
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.
}
}