我制作了一个careView来显示像博客一样的Image Banner。
<CarouselView ItemsSource="{Binding Banner}">
<CarouselView.ItemTemplate>
<DataTemplate>
<Image Source="{Binding ImageUrl}"/>
</DataTemplate>
</CarouselView.ItemTemplate>
并给ItemSsource像:
public ObservableCollection<ImageData> Banner ;
Banner = new ObservableCollection<ImageData>
{
new ImageData()
{
ImageUrl = "b1.png",
ImageWidth = (int)App.UIWidth,
ImageHeight=(int)App.UIHeight
},
new ImageData()
{
ImageUrl="b2.png",
ImageWidth=(int)App.UIWidth,
ImageHeight=(int)App.UIHeight
}
};
我认为这是正确的,但总是会出错:指定的演员表是无效的。
我尝试了很多解决方案。绑定环境无疑是正确的。请告诉我哪里出错了。
我的xamarin版本:3.6.0.264807
我创建了关于使用CarouselView的简单演示,我没有任何问题。
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="test2.Page15"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:forms="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.CarouselView">
<ContentPage.Content>
<StackLayout>
<forms:CarouselView ItemsSource="{Binding banners}">
<forms:CarouselView.ItemTemplate>
<DataTemplate>
<Image Source="{Binding imageurl}" />
</DataTemplate>
</forms:CarouselView.ItemTemplate>
</forms:CarouselView>
</StackLayout>
</ContentPage.Content>
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Page15 : ContentPage
{
public ObservableCollection<model11> banners { get; set; }
public Page15 ()
{
InitializeComponent ();
banners = new ObservableCollection<model11>()
{
new model11(){imageurl="a11.jpg"},
new model11(){imageurl="c1.jpg"},
new model11(){imageurl="c2.jpg"},
new model11(){imageurl="c3.jpg"}
};
this.BindingContext = this;
}
}
public class model11
{
public string imageurl { get; set; }
}