首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何确定carseview的Itemsource?

如何确定carseview的Itemsource?
EN

Stack Overflow用户
提问于 2019-04-02 11:19:01
回答 1查看 52关注 0票数 0

我制作了一个carseView来显示像博客一样的图像横幅。

代码语言:javascript
复制
<CarouselView ItemsSource="{Binding Banner}">
            <CarouselView.ItemTemplate>
                <DataTemplate>
                    <Image Source="{Binding ImageUrl}"/>
                </DataTemplate>
            </CarouselView.ItemTemplate>

并给ItemSource如下:

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-05 03:39:47

我创建了关于使用CarouselView的简单演示,我没有任何问题。

代码语言:javascript
复制
<?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>

代码语言:javascript
复制
[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; }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55473495

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档