【问题标题】:Xamarin Empty CarouselViewXamarin 空轮播视图
【发布时间】:2017-10-27 13:21:11
【问题描述】:

我正在尝试向我的 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

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

【问题讨论】:

  • 为了与您的列表绑定工作,您需要确保您可以“获取”它: List list { get; } = 新列表();此外,您目前只向列表中添加一项 - 因此您只有 1 项可查看
  • 该声明出现错误“;预期”,我只想查看一项,因为这只是一个测试...
  • 对不起,应该提到你应该在构造函数之外声明它,并将其公开
  • 仍然没有运气:有 2 个项目我有一个奇怪的类似滚动的视图但没有标签......我认为问题出在框架本身
  • 是“label_nome”在你的 Oggetto_spesa 类中设置为 public 和 get?我显然怀疑存在绑定问题,如果您只是将 xaml 中的文本分配更改为随机字符串会发生什么?

标签: c# android ios xamarin xamarin.forms


【解决方案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 .Add(new 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;
    }

【讨论】:

    【解决方案2】:

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

    你的轮播视图代码没问题,绑定需要标记为公开。

    改变

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

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

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

    同样在“Oggetto_spesa”类中确保“label_nome”是公开的。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-17
      • 1970-01-01
      • 1970-01-01
      • 2022-11-04
      • 1970-01-01
      相关资源
      最近更新 更多