Xamarin DataTemplate的数据绑定问题

时间:2018-09-07 07:08:41

标签: c# xaml xamarin data-binding

好吧,我是Xamarin的新手,对此有点挣扎。我正在尝试创建带有“页面”或“卡片”轮播的页面。它实际上可以正常工作,但是我无法在其中发布动态数据(名称,schoolClass和课程)?

这就是我想要做的:

ObservableCollection<Details> collection = new ObservableCollection<Details>();

collection.Add(new Details { name = "Peter Hanson", schoolClass = "4. klasse", course = "Math" });
collection.Add(new Details { name = "Tom Jerk", schoolClass = "7. klasse", course = "Geography" });
collection.Add(new Details { name = "Caroline Jump", schoolClass = "1. klasse", course = "English" });

// Carousel pages template
DataTemplate template = new DataTemplate(() =>
{

    var stacksample = new StackLayout()
    {
        HorizontalOptions = LayoutOptions.FillAndExpand,
        VerticalOptions = LayoutOptions.FillAndExpand,
        BackgroundColor = Color.White
    };

    stacksample.Children.Add(new Label
    {
        Text = "{Binding name}",
        HorizontalOptions = LayoutOptions.Center
    });

    stacksample.Children.Add(new Image
    {
        Source = ImageSource.FromResource("md.Images.default.png",
        typeof(EmbeddedImage).GetTypeInfo().Assembly)
    });

    stacksample.Children.Add(new Label
    {
        Text = "{Binding schoolClass}",
        HorizontalOptions = LayoutOptions.Center,
        Style = Headline
    });

    stacksample.Children.Add(new Label
    {
        Text = "{Binding course}",
        HorizontalOptions = LayoutOptions.Center,
        Margin = new Thickness(0, -8, 0, 0),
        Style = SubStyle
    });

    return stacksample;

});

carousel.ItemTemplate = template;
carousel.PositionSelected += pageChanged;
carousel.ItemsSource = collection;

我希望有人能在正确的方向上帮助或指导我,并预先致谢:-)

0 个答案:

没有答案