Xamarin空CarouselView

问题描述 投票:0回答:2

我正在尝试将旋转木马视图添加到我的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

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

c# android ios xamarin xamarin.forms
2个回答
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;
    }

0
投票

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

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

更改

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

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

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

同样在您的“Oggetto_spesa”类中,确保“label_nome”是公共的。

仅供将来参考,如果你想更新轮播视图的任何细节(我假设你使用Bindings后你做了),你应该将List更改为ObservableCollection ObservableCollection<> vs. List<>

© www.soinside.com 2019 - 2024. All rights reserved.