【发布时间】: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