В простом примере с использованием форм 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();
}
}
}