【问题标题】:Xamarin Forms: How to implement an image carousel in 2019Xamarin Forms:如何在 2019 年实现图像轮播
【发布时间】:2019-06-03 20:59:20
【问题描述】:
2019 年有没有人成功实现过图片滑块?我发现的所有解决方案似乎都引用了一些不再可用的过时 Nuget 包。基本上,我想在我的 ContentPage 中添加一个部分,其中背景图像将不断变化(可能以定时方式)。
【问题讨论】:
标签:
android
ios
visual-studio
xamarin.forms
【解决方案1】:
您需要基于水平滚动视图创建自己的控件。例如,像这样创建 ContentView(使用 xaml):
public partial class View1 : ContentView
{
public static readonly BindableProperty CollectionProperty = BindableProperty.Create(nameof(Collection), typeof(List<ProxyObject>),
typeof(View1), default(List<ProxyObject>), BindingMode.OneWay, propertyChanged: OnCollectionPropertyChanged);
private static void OnCollectionPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
if (!(bindable is View1 view1))
{
return;
}
foreach (var item in newValue as List<ProxyObject>)
{
view1.stackLayout.Children.Add(new Label());
}
}
public List<ProxyObject> Collection
{
get { return (List<ProxyObject>)GetValue(CollectionProperty); }
set { SetValue(CollectionProperty, value); }
}
public View1()
{
InitializeComponent();
}
}
在 xaml 中:
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TcuClientStandard.Helpers.Views.View1">
<ContentView.Content>
<ScrollView Orientation="Horizontal">
<StackLayout x:Name="stackLayout" Orientation="Horizontal">
</StackLayout>
</ScrollView>
</ContentView.Content>
</ContentView>
这是一个供参考的示例。