当前位置: 首页 > 编程语言 > C#

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

时间:2023-04-10 11:22:48 C#

C#学习教程:Xamarindatatemplatewithbindingbehindincodenotworking我正在使用Nuget包、DLToolkit、流程列表来创建流程列表。在使用Xaml之前,我已经在我的项目中使用过这个包,并且它工作得很好。然而,当我尝试在后面的代码中创建一个数据模板时,它只显示一个空白控件,但是当将鼠标悬停在该控件上时,您可以看到其中有实际项目。我的问题是:如何在代码隐藏的数据绑定中创建数据模板?下面是适用于Xaml的示例:但是,控件是在此项目中生成的,因此不涉及Xaml代码。这是我在代码中尝试过的代码示例,但没有起用:#regionDatatemplatevardataTemplate=newDataTemplate(()=>{varStackLayout=newStackLayout{BackgroundColor=Color.Pink,Padding=2,Horizo??ntalOptions=LayoutOptions.FillAndExpand};#regionchildren/contentforframeAbsoluteLayoutabsoluteLayout=newAbsoluteLayout{Horizo??ntalOptions=LayoutOptions.FillAndExpand,VerticalOptions=LayoutOptions.FillAndExpand};#regioncontentforAbsoluteLayoutvarimgBg=newImage();AbsoluteLayout.SetLayoutBounds(imgBg,new矩形(1、1、1、1));AbsoluteLayout.SetLayoutFlags(imgBg,AbsoluteLayoutFlags.All);imgBg.SetBinding(Image.SourceProperty,“BgImage”);absoluteLayout.Children.Add(imgBg);varoverlayBox=newBoxView{Color=Color.Black,Opacity=0.5};AbsoluteLayout.SetLayoutBounds(overlayBox,newRectangle(1,1,1,1));AbsoluteLayout.SetLayoutFlags(overlayBox,AbsoluteLayoutFlags.All);absoluteLayout.Children.Add(overlayBox);#regionInnerStackpanelStackLayoutinnerStackVoorAbsoluteLayout=newStackLayout{Margin=newThickness(20),VerticalOptions=LayoutOptions.CenterAndExpand,Horizo??ntalOptions=LayoutOptions.CenterAndExpand};varlblTitel=newLabel{FontSize=Device.GetNamedSize(NamedSize.Medium,typeof(Label)),TextColor=Color.White};varlblSubTitel=newLabel{FontSize=Device.GetNamedSize(NamedSize.Medium,typeof(Label)),TextColor=Color.White};//绑定lblTitel.SetBinding(Label.TextProperty,"Title");lblSubTitel.SetBinding(Label.TextProperty,"副标题");innerStackVoorAbsoluteLayout.Children.Add(lblSubTitel);innerStackVoorAbsoluteLayout.Children.Add(lblTitel);absoluteLayout.Children.Add(innerStackVoorAbsoluteLayout);#endregion#endregion#endregion框架frame=newFrame();frame.Content=absoluteLayout;StackLayout.Children.Add(框架);返回堆栈布局;});#endregionFlowListViewlstRelatieLijst=newFlowListView();lstRelatieLijst.ItemsSource=lst相关项目;lstRelatieLijst.FlowColumnTemplate=dataTemplate;lstRelatieLijst.BackgroundColor=Color.LightGoldenrodYellow;lstRelatieLijst.FlowColumnCount=1;lstRelatieLijst.HasUnevenRows=true;?我已经尝试了以下来源,但不幸的是我没有按预期工作我希望看到相同的结果或类似XAML代码的东西。但是在关注他们的信息之后,FLowListView似乎是空的:https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/templates/data-templates/creatinghttps://www.codeproject.com/Questions/516614/createplusdatatemplatepluscodeplusbehind你应该使用flowList.SetBinding(FlowListView.FlowItemsSourceProperty,"List");而不是ItemsSource这里是工作示例....上面是C#学习教程:Xamarindatatemplatewithbindingincodebehind我无法正常工作并共享所有内容。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注——usingDLToolkit.Forms.Controls;使用系统;使用Xamarin.Forms;namespaceFlowListTest{publicpartialclassMainPage:ContentPage{publicMainPage(){InitializeComponent();加载UI();BindingContext=newBContext();}privatevoidLoadUI(){vardataTemplate=newDataTemplate(()=>{varimage=newImage();image.SetBinding(Image.SourceProperty,"BgImage");vartitleLabel=newLabel{FontSize=Device.GetNamedSize(NamedSize.Medium,typeof(Label)),TextColor=Color.White,};titleLabel.SetBinding(Label.TextPr财产,“标题”);varsubTitleLabel=newLabel{FontSize=Device.GetNamedSize(NamedSize.Medium,typeof(Label)),TextColor=Color.White,};subTitleLabel.SetBinding(Label.TextProperty,"字幕");返回新的StackLayout{BackgroundColor=Color.Pink,Padding=2,Horizo??ntalOptions=LayoutOptions.FillAndExpand,Children={newFrame{Content=newAbsoluteLayout{Horizo??ntalOptions=LayoutOptions.FillAndExpand,VerticalOptions=LayoutOptions.FillAndExpand,Children={image,newStackLayout{Margin=newThickness(20),VerticalOptions=LayoutOptions.CenterAndExpand,Horizo??ntalOptions=LayoutOptions.CenterAndExpand,Children={titleLabel,subTitleLabel}}}}}}};});varflowList=newFlowListView();flowList.SetBinding(FlowListView.FlowItemsSourceProperty,"列表");flowList.FlowColumnTemplate=dataTemplate;flowList.BackgroundColor=Color.LightGoldenrodYellow;flowList.FlowColumnCount=1;flowList.HasUnevenRows=true;变量button=newButton{Text="Add"};button.Clicked+=Button_Clicked;Content=newStackLayout{Children={button,flowList}};}privatevoidButton_Clicked(objectsender,EventArgse){(BindingContextasBContext).Add();}}publicclassFoo{publicstringBgImage{get;放;}公共字符串标题{得到;放;}publicstring副标题{get;放;}}publicclassBContext{publicFlowObservableCollectionList{get;放;}publicBContext(){List=newFlowObservableCollection{newFoo{BgImage="http://via.placeholder.com/350x150",Title="Title",Subtitle="SubTitle"},newFoo{BgImage="http://via.placeholder.com/350x150",Title="Title1",Subtitle="SubTitle1"}};}publicvoidAdd(){List.Add(newFoo{BgImage="http://via.placeholder.com/350x150",Title="Title"+List.Count,Subtitle="SubTitle"+List.Count});}}}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除如需转载请注明出处: