在Xamarin表单中,如果已经来自父列表视图,如何将子集合传递到列表视图中使用的新页面?

问题描述 投票:1回答:2

所以这是场景。我有一个父列表视图,其模型包含另一个集合,我想将其传递给另一个页面,以便在单击标签时在另一个列表视图中使用。

父ViewModel如下:

    public ObservableCollection<Songs> JazzCollection { get; set; }
    public RecordsViewModel()
    {
        JazzCollection = new ObservableCollection<Songs>();
        var playlist = new Songs
        {
            Station = "Jazz Legends",
            Artists = new ObservableCollection<Artist>
            {
                new Artist
                {
                    ArtistID = 0,
                    Name = "John Coltrane",
                    Song = "Giant Steps"
                },
                new Artist
                {
                    ArtistID = 1,
                    Name = "Miles Davis",
                    Song = "So What"
                },
                new Artist
                {
                    ArtistID = 2,
                    Name = "Blue Mitchell",
                    Song = "Kinda Blue"
                },
            },
            Title = "Click here to see artists in playlist"
        };
        JazzCollection.Add(playlist);

        var anotherPlaylist
            .
            .
            .
        JazzCollection.Add(anotherPlaylist);
    }

现在父XAML是:

<ListView x:Name="RecordsListView"
          SeparatorVisibility="None"
          HasUnevenRows="true"
          ItemsSource="{Binding JazzCollection}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Label Text="{Binding Station}"/>
                    <!--WHEN THEY CLICK ON THIS TITLE BELOW
                        I'D LIKE THEM TO GO TO A NEW PAGE LISTING
                        THE ARTISTS IN THIS STATION. HOW DO I
                        BIND THE ARTISTS ObservableCollection TO
                        THE NEW PAGE? -->
                    <Label Text="{Binding Title}"/>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

列表视图显示父级没有任何问题,但我很难通过子集合“艺术家”发送以在新页面中用作BindingContext。任何想法如何做到这一点?

这是我计划为新页面Viewmodel做的事情,但我不确定它是对的。

public class ArtistViewModel
{
    public ObservableCollection<Artist> Artists { get; set; }
    public ArtistViewModel(ObservableCollection<Artist> artists)
    {
        Artists = artists;
    }
}

然后我在第二页的新ListView中使用“Artists”作为ItemSource。

c# listview xamarin.forms
2个回答
1
投票

添加一个ItemSelected处理程序到您的ListView

void OnSelection (object sender, SelectedItemChangedEventArgs e)
{
  if (e.SelectedItem == null) {
    return; //ItemSelected is called on deselection, which results in SelectedItem being set to null
  }

  // get the selected Songs object
  var songs = (Songs)e.SelectedItem;

  // pass the Artists collection to the next page
  Navigation.PushAsync(new ArtistsPage(songs.Artists));
}

0
投票

答案结果是TapGestureRecognizer,但需要在Command中包含一些其他东西才能正常工作,因为它包含在ListView中。它应该看起来像这样:

<Label.GestureRecognizers>
      <TapGestureRecognizer
            Command="{Binding Path=BindingContext.TapCommand, Source={x:Reference JazzCollection}}"
            CommandParameter="{Binding Comments}"
            NumberOfTapsRequired="1"/>
</Label.GestureRecognizers>

有一点需要注意,x:Reference JazzCollection是指给ContentPage的名称。必须将其添加到顶级XML标记:x:Name =“JazzCollection”。这是我用作参考的答案的链接。请参阅JamesMontemagno的评论约10条评论。

参考文献:

https://forums.xamarin.com/discussion/57539/listview-with-mvvm-tapgesturerecognizer-not-working

https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/gestures/tap/

© www.soinside.com 2019 - 2024. All rights reserved.