Xamarin空CarouselView

时间:2017-10-27 13:21:12

标签: c# android ios xamarin xamarin.forms

我正在尝试将轮播视图添加到我的xamarin表单跨平台应用程序中:

这是我的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" 
    xmlns:cv="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.CarouselView" 
    x:Class="test.Pagina3">


    <ContentPage.Content>
        <StackLayout HorizontalOptions="Center" VerticalOptions="Center" WidthRequest="300" 
            HeightRequest="190">
            <cv:CarouselView x:Name="carosello" BackgroundColor="Yellow">
                <cv:CarouselView.ItemTemplate>

                    <DataTemplate>

                        <StackLayout HorizontalOptions="Center" VerticalOptions="Center">

                            <Label x:Name = "label_nome" 
                                Text = "{Binding nome}" 
                                TextColor = "Black" />

                        </StackLayout>
                    </DataTemplate>

                </cv:CarouselView.ItemTemplate>
            </cv:CarouselView>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

这是我的视图模型代码:

   using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

using Xamarin.Forms;

namespace test
{
    public partial class Pagina3 : ContentPage
    {
        public Pagina3()
        {
            InitializeComponent();

            //ViewModelSpesa spesa = new ViewModelSpesa();
            List<Oggetto_spesa> list = new List<Oggetto_spesa>();

            list.Add(new Oggetto_spesa("jsna", 123.1, "13sd"));
            carosello.ItemsSource = list;
        }

    }
}

结果是一个空的黄色视图,我已经看到了其他问题,例如: Xamarin CarouselView Not Displayed

没有运气,我做错了什么?

2 个答案:

答案 0 :(得分:1)

Here is my Xaml code:-

<?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="MyCairns.Views.ZoomReportImage"
       xmlns:cv="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.CarouselView"
           x:Name="ZoomPage"  xmlns:local="clr-namespace:MyCairns"
        Title="{local:Copy ViewPhoto}" >
    <StackLayout x:Name="stackcarosel" >
        <cv:CarouselView x:Name="CarouselZoos"   ItemsSource="{Binding ImagesZoom}" >
            <cv:CarouselView.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <Image Source="{Binding Source}" />                    
                    </Grid>
                </DataTemplate>
            </cv:CarouselView.ItemTemplate>
        </cv:CarouselView>
    </StackLayout>

</ContentPage>

这是我的Xaml.cs代码: -

ObservableCollection<GalleryImage> _images = new ObservableCollection<GalleryImage>();

_images。添加(新的GalleryImage(“jsna”,123.1,“13sd”));

PageViewModel = new PageViewModel(_images);

这是我的查看型号代码: -

    ObservableCollection<GalleryImage> _images = new ObservableCollection<GalleryImage>();


    public ObservableCollection<GalleryImage> ImagesZoom{get => _images;}

    public PageViewModel(ObservableCollection<GalleryImage> _images)
    {
        this._images = _images;
    }

答案 1 :(得分:0)

添加评论作为答案,以便将其标记为已完成:

您的轮播视图代码很好,绑定需要标记为公开。

更改

List<Oggetto_spesa> list = new List<Oggetto_spesa>();

public List<Sensors> list { get; } = new List<Sensors>();

并确保将其移出构造函数。

同样在你的&#34; Oggetto_spesa&#34; class确保&#34; label_nome&#34;是公开的。

仅供将来参考,如果您想更新轮播视图的任何详细信息(我假设您这样做,因为您正在使用Bindings),您应该将List更改为ObservableCollection ObservableCollection<> vs. List<>