具有列表视图和详细视图的 Xamarin 表单应用程序
Xamarin forms app with listview and detailed view
我正在寻找一个解决方案,让列表视图包含列表中的项目,这是有效的,我想这样做,如果我点击列表中的项目,我会得到详细信息项目视图。
所以我的问题是,如何将详细视图绑定到所选项目。
这是我目前的数据来源
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;
}
}
这是我的主要观点
public 部分 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
}
}
}
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.
}
}
我正在寻找一个解决方案,让列表视图包含列表中的项目,这是有效的,我想这样做,如果我点击列表中的项目,我会得到详细信息项目视图。
所以我的问题是,如何将详细视图绑定到所选项目。 这是我目前的数据来源
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;
}
}
这是我的主要观点 public 部分 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
}
}
}
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.
}
}