xamarin.forms - Xamarin Forms CarouselView.ItemsSource 不使用绑定
问题描述
在使用 Xamarin Forms 的简单示例中CarouselView
,我尝试将ItemsSource
using设置Binding
为公共对象(获取属性),但它不起作用。显示CarouselView
,但没有任何数据可查看。下面是我的 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:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="CVApp.OtherPage">
<ContentPage.Content>
<StackLayout>
<CarouselView x:Name="_carouselView" ItemsSource="{Binding Stuffs}">
<CarouselView.ItemTemplate>
<DataTemplate>
<StackLayout>
<Frame
BorderColor="DarkGray"
CornerRadius="5"
Margin="20"
HeightRequest="50"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand">
<StackLayout>
<Label Text="{Binding Text}" />
</StackLayout>
</Frame>
</StackLayout>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
using System;
using System.Collections.Generic;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace CVApp
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class OtherPage : ContentPage
{
private List<Stuff> _stuffs;
public List<Stuff> Stuffs
{
get
{
return this._stuffs;
}
}
public OtherPage()
{
InitializeComponent();
_stuffs = new List<Stuff>();
for (int i = 0; i < 10; i++)
{
_stuffs.Add(new Stuff(i));
}
}
}
public class Stuff
{
public string Text { get; set; }
public Stuff(int i)
{
this.Text = i.ToString();
}
}
}
但是,如果我只是ItemsSource="{Binding Stuffs}"
从 Xaml 代码中删除,而是ItemsSource
直接在代码隐藏中设置,它工作正常(见下面的代码)。谁能告诉我我的绑定代码有什么问题?
<?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:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="CVApp.OtherPage">
<ContentPage.Content>
<StackLayout>
<CarouselView x:Name="_carouselView" ItemsSource="{Binding Stuffs}">
<CarouselView.ItemTemplate>
<DataTemplate>
<StackLayout>
<Frame
BorderColor="DarkGray"
CornerRadius="5"
Margin="20"
HeightRequest="50"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand">
<StackLayout>
<Label Text="{Binding Text}" />
</StackLayout>
</Frame>
</StackLayout>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
using System;
using System.Collections.Generic;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace CVApp
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class OtherPage : ContentPage
{
private List<Stuff> _stuffs;
public List<Stuff> Stuffs
{
get
{
return this._stuffs;
}
}
public OtherPage()
{
InitializeComponent();
_stuffs = new List<Stuff>();
for (int i = 0; i < 10; i++)
{
_stuffs.Add(new Stuff(i));
}
this._carouselView.ItemsSource = this._stuffs;
}
}
public class Stuff
{
public string Text { get; set; }
public Stuff(int i)
{
this.Text = i.ToString();
}
}
}
解决方案
您应该添加BindingContext = this;
Page 的构造函数。
这是代码。
public partial class MainPage : ContentPage
{
private List<Stuff> _stuffs;
public List<Stuff> Stuffs
{
get
{
return _stuffs;
}
}
public MainPage()
{
InitializeComponent();
_stuffs = new List<Stuff>();
for (int i = 0; i < 10; i++)
{
_stuffs.Add(new Stuff(i));
}
BindingContext = this;
}
public class Stuff
{
public string Text { get; set; }
public Stuff(int i)
{
this.Text = i.ToString();
}
}
}
这里正在运行 GIF。
推荐阅读
- comments - 如何使用 SPOON 获得评论?
- python-3.x - Python Webdriver - Firefox 配置文件 - 错误代码:SEC_ERROR_UNKNOWN_ISSUER -
- python - 将 JSON 导入 pandas 数据框错误
- python - 实例方法作为函数对象
- javascript - 有没有办法在Javascript中做动态CSG?
- javascript - 检测用户是否在 JS 中使用 Brave
- excel - 财政年度从 4 月开始,但我想使用 ISO 2018 日历年的第 14 周到 2019 日历年的第 13 周
- javascript - AJAX:禁用提交按钮,直到用户名字段充满电子邮件
- html - 图片未显示在 Jekyll 帖子中,但显示在本地
- c# - REST API 调用显示 404 错误