我制作了一个carseView来显示像博客一样的图像横幅。
<CarouselView ItemsSource="{Binding Banner}">
<CarouselView.ItemTemplate>
<DataTemplate>
<Image Source="{Binding ImageUrl}"/>
</DataTemplate>
</CarouselView.ItemTemplate>
并给ItemSource如下:
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
}
};我认为这是正确的,但总是会出现错误:指定的强制转换无效。
我尝试过许多solutions.The绑定上下文无疑是正确的。请告诉我哪里出错了。
我的xamarin版本:3.6.0.264807
发布于 2019-04-05 03:39:47
我创建了关于使用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; }
}https://stackoverflow.com/questions/55473495
复制相似问题