如何解决滑动手势仅在 xamarin 形式的标题中
我需要一个包含标题和内容的视图。 假设应用显示水果名称及其详细信息。
假设第一个视图将具有标题 -“草莓”,内容将是草莓的图片和详细信息。我只需要在标题“草莓”上执行滑动操作,以便在滑动它时,下一个项目“芒果”出现在标题中,下面是芒果的详细信息。
所以现在标题“Mango”必须可以左右滑动。向右滑动查看上一个水果草莓的详细信息或向左滑动查看下一个水果-橙子的详细信息。
如果橙色是最后一个水果,它应该在它的标题中有滑动操作,只能查看上一个图像,因为它没有任何显示为下一个项目。
我有一个列表中的所有水果名称和其他详细信息。请告诉我如何实现这一目标
解决方法
最简单的方法是使用 CarouselView 作为标题。
一个简单的例子,如下所示:
页面 xaml:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="NewForms.CollectionViewPage">
<ContentPage.Content>
<StackLayout>
<CarouselView x:Name="collection" ItemSource={Binding Fruits} HeightRequest="50" Loop="False">
<CarouselView.ItemTemplate>
<DataTemplate>
<Label Text="{Binding Name}"></Label> //this for the header content
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
<StackLayout Orientation="Vertical" BindingContext="{Binding Path=CurrentItem,Source={x:Reference collection}}">
<Label Text="{Binding Details}"></Label> //binding the details content
</StackLayout>
</StackLayout>
</ContentPage.Content>
</ContentPage>
page.xaml.cs:
public CarouselViewPage()
{
InitializeComponent();
FruitModel fruitModel = new FruitModel();
BindingContext = fruitModel;
}
视图模型:
class FruitModel
{
public ObservableCollection<Fruit> Fruits { get; set; }
public FruitModel()
{
Fruits = new ObservableCollection<Fruit>();
Fruits.Add(new Fruit() { Name = "Apple",Details = "Thi is an apple" });
Fruits.Add(new Fruit() { Name = "Pear",Details = "Thi is a Pear" });
Fruits.Add(new Fruit() { Name = "Banana",Details = "Thi is a Banana" });
Fruits.Add(new Fruit() { Name = "Strawberry",Details = "Thi is a Strawberry" });
}
}
型号:
class Fruit
{
public string Name { get; set; }
public string Details { get; set; }
}
您可以根据需要更改模板和数据库。
,Xamarin.Forms 提供滑动手势,您可以将滑动手势应用于标题。
https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/gestures/swipe
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。