【问题标题】:Xamarin CarouselView and the List -> Detail paradigmXamarin CarouselView 和列表 -> 详细信息范例
【发布时间】:2022-01-21 20:06:53
【问题描述】:

想在使用 CarouselView 导航范式时如何显示和从列表视图导航到详细信息视图来开动脑筋。

具体来说,假设您有一个这样的轮播页面:

<ContentPage>
    <Grid>
       <CarouselView>
           <CarouselView.ItemsSource>
               <x:Array Type="{x:Type ContentView}">
                  <c:View0 />
                  <c:View1 />
                  <c:View2 />
           </CarouselView.ItemsSource>
       </CarouselView>
   </Grid>
</ContentPage>

还假设轮播中的一个视图(例如 View1)包含从您的数据库中获取的产品列表,例如:

<CollectionView ItemsSource="{Binding listofproducts}" SelectionChanged="OnItemClicked">
     <CollectionView.ItemTemplate>
         <DataTemplate>
             <StackLayout>
                 <Label Text="{Binding productname}"
             </StackLayout>
        </DataTemplate>
     </CollectionView.ItemTemplate>
</CollectionView>

当用户触发 OnItemClicked() 时,您将如何显示产品的详细信息页面?如果你打开一个详细视图,那会不会混淆 CarouselView 导航逻辑?以及如何进入列表页面?

不确定哪种方法可以在这里奏效。 有什么想法/例子吗?

提前致谢!

【问题讨论】:

  • 我不确定您认为 Carousel 会如何“混淆”?只需做一个 PushAsync - 我不明白为什么这不起作用。虽然在轮播中拥有一个列表对我来说似乎是糟糕的用户体验 - 我更担心会混淆用户。
  • 这就是我的意思,混淆用户,而不是控件本身。问题是,我真的很喜欢 Carousel 范式,我想让它与 List -> Detail 范式一起工作,但我不知道如何让这两个概念融合在一起。
  • 我会尝试一下,看看用户的行为是什么样的
  • 我做到了,这令人困惑。一旦用户进入详细信息页面,他可以从那里去哪里并不明显。令我惊讶的是,在我看来,这似乎是使用轮播导航的一个相当常见的使用场景,但我还没有找到任何示例或尝试。

标签: listview xamarin carousel detailview


【解决方案1】:

根据你的需要我写了一个小例子供你参考。

首先在xaml页面上创建一个CarouselView,在里面添加一个CollectionView并添加一个点击事件。

这里是 xaml 代码:

<StackLayout>
    <CarouselView ItemsSource="{Binding Mydates}">
        <CarouselView.ItemTemplate>
            <DataTemplate>
                <StackLayout>
                    <Frame HasShadow="True"
                   BorderColor="DarkGray"
                   CornerRadius="5"
                   Margin="20"
                   HeightRequest="300"
                   HorizontalOptions="Center"
                   VerticalOptions="CenterAndExpand">
                        <CollectionView  ItemsSource="{Binding data2}">
                            <CollectionView.ItemTemplate>
                                <DataTemplate>
                                    <StackLayout>
                                        <StackLayout.GestureRecognizers >
                                            <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"/>
                                        </StackLayout.GestureRecognizers>
                                        <Label Text="{Binding .}"/>
                                    </StackLayout>
                                </DataTemplate>
                            </CollectionView.ItemTemplate>
                        </CollectionView>
                    </Frame>
                </StackLayout>
            </DataTemplate>
        </CarouselView.ItemTemplate>
    </CarouselView>
</StackLayout>

后台方法可以获取点击信息,然后可以进行其他逻辑判断。

后台代码如下:

public partial class MainPage : ContentPage
{
     
    public ObservableCollection<Class1> Mydates { get; set; } = new ObservableCollection<Class1>();
    public MainPage()
    {
        InitializeComponent();
        Mydates.Add(new Class1() { data1 = "aaa",data2 = new List<string>() { "aaa","bbb","cccc"} });
        Mydates.Add(new Class1() { data1 = "bbb", data2 = new List<string>() { "ddd", "eeee", "ffffff" } });
        Mydates.Add(new Class1() { data1 = "ccc", data2 = new List<string>() { "ggg", "hhhh", "iii" } });
        BindingContext = this;
    }


    private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
    {
        var sta = sender as StackLayout;
        var mydata = (sta.Children[0] as Label).Text;
    }
}

【讨论】:

  • 有趣的方法,谢谢。
  • 问题解决了吗?
  • 嗯,这不是什么大问题,而是邀请考虑不同的方法。你的方法很有趣,谢谢。让我们看看其他人是否有其他想法。
猜你喜欢
  • 1970-01-01
  • 2016-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多