Xamarin使用listview和详细视图构建应用程序

时间:2017-03-07 23:32:46

标签: 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 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 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
        }

    }
}

由于 link to project on git

1 个答案:

答案 0 :(得分:0)

杰森在那里你的代码是正确的是正确的,但也许这可以清除你的想法:

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.
    }   
}