Xamarin Forms CarouselView.ItemsSource 不使用绑定

Xamarin Forms CarouselView.ItemsSource not working with Bindings

在一个使用 Xamarin Forms CarouselView 的简单示例中,我尝试使用 BindingItemsSource 设置为 public 对象(获取 属性) ,但它不起作用。 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();
        }
    }
}

但是,如果我只是从 Xaml 代码中删除 ItemsSource="{Binding Stuffs}",而是直接在代码隐藏中设置 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;

这是代码。

    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。