【问题标题】:Xamarin forms select carousel itemXamarin 表单选择轮播项目
【发布时间】:2020-12-29 19:02:03
【问题描述】:

我正在学习 Xamarin,我想获取所选轮播项目的值。我无法在列表视图中添加 ItemSelected。

这是我的 Xaml 代码:

<CarouselView x:Name="NewsList"  >
    <CarouselView.ItemTemplate >
        <DataTemplate>
            <StackLayout>
                <Frame HasShadow="True"
                       BorderColor="DarkGray"
                       CornerRadius="5"
                       Margin="20"

                       HeightRequest="300"
                       HorizontalOptions="Center"
                       VerticalOptions="CenterAndExpand">
                                            <StackLayout>
                                                <Label Text="{Binding NewsName}"
                               FontAttributes="Bold"
                               FontSize="Large"
                               HorizontalOptions="Center"
                               VerticalOptions="Center" />
                                                <Image Source="{Binding NewsImageUrl}"
                               Aspect="Fill"
                               HeightRequest="150"
                               WidthRequest="150"
                               HorizontalOptions="Center" />
                                                <Label Text="{Binding ProviderAndDate}"
                               HorizontalOptions="Center" />

                                            </StackLayout>
                                        </Frame>
            </StackLayout>
        </DataTemplate>
    </CarouselView.ItemTemplate>
</CarouselView>

这是我选择项目后想要使用的功能:获取 NewsName 和 ProviderAndDate 的值

  private void OnFrameTapped(object sender, EventArgs e)
        {
            Console.WriteLine("SelectedNewsName" + "SelectedNewsProviderAndDate");
        }

感谢您的帮助

【问题讨论】:

    标签: c# xaml xamarin.forms


    【解决方案1】:

    CarouselView 在可视化和事件方面与 ListView 有点不同,因此您也必须想出不同的解决方案

    在此处执行类似于所选项目事件的最简单方法是需要您进行自定义以添加事件。

    一个好的解决方法是只使用 CurrentItem 属性或在框架的 Tap 上传递一个 CommandParameter!

    <StackLayout>
       <StackLayout.GestureRecognizers>
       <TapGestureRecognizer Command="{Binding CarouselItemTapped,Source={x:Reference currentPage}}" CommandParameter="{Binding .}"/>
       </StackLayout.GestureRecognizers>
    <Frame ...... />
    

    然后给你当前的内容页一个x:Name

    <ContentPage ..... X:Name="currentPage"
    

    现在在你的xaml.cs

    Public ICommand CarouselItemTapped{ get; set; }
    

    在构造函数中:

    CarouselItemTapped= new Xamarin.Forms.Command((selectItem)=>{//Perform action here});
    

    如果您有任何问题,请随时回复

    关于命令的更多信息:https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/commanding

    更多关于 CarouselView:https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/carouselview/

    【讨论】:

      【解决方案2】:

      晚了,但它可能会帮助某人。使用 BindingContext 绑定数据的人,可以得到如下图的 tapped 模型。

      private void OnFrameTapped(object sender, EventArgs e)
      {
          var frame = sender as Frame;
          var x = frame.BindingContext as YOURMODEL;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-04-18
        • 2020-08-04
        • 1970-01-01
        • 1970-01-01
        • 2023-03-10
        • 2019-09-26
        • 1970-01-01
        相关资源
        最近更新 更多