Xamarin Forms CarouselView.ItemsSource не работает с привязками - PullRequest
0 голосов
/ 20 января 2020

В простом примере с использованием форм Xamarin CarouselView я пытаюсь установить ItemsSource с использованием Binding для объекта c publi (свойство get), но он не работает. 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 непосредственно в выделенном фрагменте кода, он работает нормально (см. код ниже). Может кто-нибудь посоветовать мне, что не так с моим кодом Bindings?

<?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();
        }
    }
}

1 Ответ

0 голосов
/ 20 января 2020

Вы должны добавить 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.

enter image description here

...