【问题标题】:Xamarin Forms CarouselView.ItemsSource not working with BindingsXamarin Forms CarouselView.ItemsSource 不使用绑定
【发布时间】:2020-01-20 00:47:48
【问题描述】:

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

标签: xamarin.forms data-binding itemssource


【解决方案1】:

您应该在页面的构造函数中添加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。

【讨论】:

  • @John,好的,请不要忘记将其标记为答案(单击此答案左上方的✔),它将帮助有类似问题的其他人。
猜你喜欢
  • 2023-03-24
  • 1970-01-01
  • 2017-08-18
  • 1970-01-01
  • 1970-01-01
  • 2021-10-10
  • 1970-01-01
  • 2018-03-01
  • 2018-01-03
相关资源
最近更新 更多