Xamarin datatemplate与代码中的绑定背后无法正常工作

我试图在我的应用程序中创建一个页面,其中所有控件都是通过后面的C#代码动态生成的。 我正在使用Nuget Packages, DLToolkit, flowlist来创建流列表。

在使用Xaml之前,我已经在我的项目中使用了这个包,它完全有效。 但是,当我尝试在后面的代码中创建一个datatemplate时,它只显示一个空白控件,但是当hover在此控件上方时,您可以看到其中有实际的项目。

我的问题是:如何在代码后面的数据绑定中创建数据模板?

这是一个示例,适用于Xaml

            

但是,在此项目中生成控件,因此不涉及Xaml代码。 这是我在代码中尝试过的代码示例,但不起作用:

 #region Datatemplate var dataTemplate = new DataTemplate(() => { var StackLayout = new StackLayout { BackgroundColor = Color.Pink, Padding = 2, HorizontalOptions = LayoutOptions.FillAndExpand }; #region children/content for frame AbsoluteLayout absoluteLayout = new AbsoluteLayout { HorizontalOptions = LayoutOptions.FillAndExpand, VerticalOptions = LayoutOptions.FillAndExpand }; #region content for AbsoluteLayout var imgBg = new Image(); AbsoluteLayout.SetLayoutBounds(imgBg , new Rectangle(1, 1, 1, 1)); AbsoluteLayout.SetLayoutFlags(imgBg , AbsoluteLayoutFlags.All); imgBg .SetBinding(Image.SourceProperty, "BgImage"); absoluteLayout.Children.Add(imgBg ); var overlayBox = new BoxView { Color = Color.Black, Opacity = 0.5 }; AbsoluteLayout.SetLayoutBounds(overlayBox, new Rectangle(1, 1, 1, 1)); AbsoluteLayout.SetLayoutFlags(overlayBox, AbsoluteLayoutFlags.All); absoluteLayout.Children.Add(overlayBox); #region InnerStackpanel StackLayout innerStackVoorAbsoluteLayout = new StackLayout { Margin = new Thickness(20), VerticalOptions = LayoutOptions.CenterAndExpand, HorizontalOptions = LayoutOptions.CenterAndExpand }; var lblTitel = new Label { FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)), TextColor = Color.White }; var lblSubTitel = new Label { FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)), TextColor = Color.White }; //Bindings lblTitel.SetBinding(Label.TextProperty, "Title"); lblSubTitel.SetBinding(Label.TextProperty, "SubTitle"); innerStackVoorAbsoluteLayout.Children.Add(lblSubTitel); innerStackVoorAbsoluteLayout.Children.Add(lblTitel); absoluteLayout.Children.Add(innerStackVoorAbsoluteLayout); #endregion #endregion #endregion Frame frame = new Frame(); frame.Content = absoluteLayout; StackLayout.Children.Add(frame); return StackLayout; }); #endregion FlowListView lstRelatieLijst = new FlowListView(); lstRelatieLijst.ItemsSource = lstRelatieItems; lstRelatieLijst.FlowColumnTemplate = dataTemplate; lstRelatieLijst.BackgroundColor = Color.LightGoldenrodYellow; lstRelatieLijst.FlowColumnCount = 1; lstRelatieLijst.HasUnevenRows = true; #endregion 

有人可以给我一些建议,我可以成为类似上面代码中的上层Xaml的东西吗?

我已经尝试了以下来源,但不幸的是我没有按预期工作。 我希望看到相同的结果或类似的东西,如XAML代码。 但在关注他们的信息后,FLowListView似乎是空的:

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/templates/data-templates/creating

https://www.codeproject.com/Questions/516614/createplusdatatemplatepluscodeplusbehind

你应该用

flowList.SetBinding(FlowListView.FlowItemsSourceProperty, "List"); 而不是ItemsSource

这是工作样本….

 using DLToolkit.Forms.Controls; using System; using Xamarin.Forms; namespace FlowListTest { public partial class MainPage : ContentPage { public MainPage() { InitializeComponent(); LoadUI(); BindingContext = new BContext(); } private void LoadUI() { var dataTemplate = new DataTemplate(() => { var image = new Image(); image.SetBinding(Image.SourceProperty, "BgImage"); var titleLabel = new Label { FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)), TextColor = Color.White, }; titleLabel.SetBinding(Label.TextProperty, "Title"); var subTitleLabel = new Label { FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)), TextColor = Color.White, }; subTitleLabel.SetBinding(Label.TextProperty, "Subtitle"); return new StackLayout { BackgroundColor = Color.Pink, Padding = 2, HorizontalOptions = LayoutOptions.FillAndExpand, Children = { new Frame { Content = new AbsoluteLayout { HorizontalOptions = LayoutOptions.FillAndExpand, VerticalOptions = LayoutOptions.FillAndExpand, Children = { image, new StackLayout { Margin = new Thickness(20), VerticalOptions = LayoutOptions.CenterAndExpand, HorizontalOptions = LayoutOptions.CenterAndExpand, Children = { titleLabel, subTitleLabel } } } } } } }; }); var flowList = new FlowListView(); flowList.SetBinding(FlowListView.FlowItemsSourceProperty, "List"); flowList.FlowColumnTemplate = dataTemplate; flowList.BackgroundColor = Color.LightGoldenrodYellow; flowList.FlowColumnCount = 1; flowList.HasUnevenRows = true; var button = new Button { Text = "Add" }; button.Clicked += Button_Clicked ; Content = new StackLayout { Children = { button, flowList } }; } private void Button_Clicked(object sender, EventArgs e) { (BindingContext as BContext).Add(); } } public class Foo { public string BgImage { get; set; } public string Title { get; set; } public string Subtitle { get; set; } } public class BContext { public FlowObservableCollection List { get; set; } public BContext() { List = new FlowObservableCollection { new Foo { BgImage = "http://via.placeholder.com/350x150", Title = "Title", Subtitle = "SubTitle" }, new Foo { BgImage = "http://via.placeholder.com/350x150", Title = "Title1", Subtitle = "SubTitle1" } }; } public void Add() { List.Add(new Foo { BgImage = "http://via.placeholder.com/350x150", Title = "Title" + List.Count, Subtitle = "SubTitle" + List.Count }); } } }