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

C0d3 0n3

所以这是场景。我有一个父级listview,其Model包含另一个集合,我想在单击标签后传递到另一个页面以在另一个列表视图中使用。

父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>

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

这是我打算为新页面Viewmodel做的事情,但我不确定这是正确的。

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

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

C0d3 0n3

答案原来是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/

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章