我正在尝试向我的xamarin forms跨平台应用程序添加一个carousel视图:
下面是我的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
如果没有运气,我做错了什么?
发布于 2018-02-28 18:16:57
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>();新.Add( _images GalleryImage("jsna",123.1,"13sd"));
PageViewModel =新的PageViewModel(_images);
以下是我的视图模型代码:
ObservableCollection<GalleryImage> _images = new ObservableCollection<GalleryImage>();
public ObservableCollection<GalleryImage> ImagesZoom{get => _images;}
public PageViewModel(ObservableCollection<GalleryImage> _images)
{
this._images = _images;
}发布于 2017-10-30 17:03:18
添加注释作为答案,以便可以将其标记为已完成:
您的carousel视图代码很好,绑定需要标记为public。
变化
List<Oggetto_spesa> list = new List<Oggetto_spesa>();至
public List<Sensors> list { get; } = new List<Sensors>();并确保将其移出构造器。
同样在你的"Oggetto_spesa“类中,确保"label_nome”是公共的。
仅供将来参考,如果您想要更新carousel视图的任何详细信息(我假设您正在使用绑定),您应该将List更改为ObservableCollection ObservableCollection<> vs. List<>
https://stackoverflow.com/questions/46976067
复制相似问题