C# 如何将ObservableCollection绑定到自定义布局?
我正在尝试构建一个名为C# 如何将ObservableCollection绑定到自定义布局?,c#,.net,xaml,xamarin,xamarin.forms,C#,.net,Xaml,Xamarin,Xamarin.forms,我正在尝试构建一个名为WrapLayout的自定义布局,它以水平而不是垂直的方式在可观察集合中打印ListItem类型的对象。我想我已经掌握了基本知识,但是我自己设置ObservableCollection时遇到了问题 我创建了一个名为GetListItems的方法,该方法返回所需的ListItems的ObservableCollection。以前,使用名为inactiveList的普通ListView时,我将绑定的数据设置为: ObservableCollection<ListItem&
WrapLayout
的自定义布局,它以水平而不是垂直的方式在可观察集合中打印ListItem
类型的对象。我想我已经掌握了基本知识,但是我自己设置ObservableCollection时遇到了问题
我创建了一个名为GetListItems
的方法,该方法返回所需的ListItems的ObservableCollection。以前,使用名为inactiveList
的普通ListView时,我将绑定的数据设置为:
ObservableCollection<ListItem> inactiveItems =
new ObservableCollection<ListItem>(
App.ListItemRepo.GetListItems());
inactiveList.ItemsSource = inactiveItems;
代码隐藏:
<ContentPage xmlns:local="clr-namespace:Myapp">
<local:WrapLayout x:Name="inactiveList" ItemsSource="{Binding .}" Spacing="5" />
public class WrapLayout : Layout<View>
{
public ObservableCollection<ListItem> ItemsSource
{
get { return (ObservableCollection<ListItem>)GetValue(ItemSourceProperty); }
set { SetValue(ItemSourceProperty, value); }
}
public static readonly BindableProperty ItemSourceProperty =
BindableProperty.Create
(
"ItemsSource",
typeof(ObservableCollection<ListItem>),
typeof(WrapLayout),
propertyChanged: (bindable, oldvalue, newvalue) => ((WrapLayout)bindable).AddViews()
);
void AddViews()
{
Children.Clear();
foreach (ListItem s in ItemsSource)
{
Label label = new Label();
label.BackgroundColor = Color.Red;
label.Text = s.Name;
label.TextColor = Color.Black;
Children.Add(label);
}
}
public static readonly BindableProperty SpacingProperty =
BindableProperty.Create
(
"Spacing",
typeof(double),
typeof(WrapLayout),
10.0,
propertyChanged: (bindable, oldvalue, newvalue) => ((WrapLayout)bindable).OnSizeChanged()
);
<!-- methods for setting styling -->
}
公共类播放:布局
{
公共可观察收集项目来源
{
get{return(ObservableCollection)GetValue(ItemSourceProperty);}
set{SetValue(ItemSourceProperty,value);}
}
公共静态只读BindableProperty ItemSourceProperty=
BindableProperty.Create
(
“项目资源”,
类型(可观测采集),
类型(播放),
propertyChanged:(可绑定、旧值、新值)=>((WrapLayout)可绑定)
);
void AddViews()
{
儿童。清除();
foreach(在ItemsSource中列出Items)
{
标签=新标签();
label.BackgroundColor=Color.Red;
label.Text=s.Name;
label.TextColor=Color.Black;
添加(标签);
}
}
公共静态只读BindableProperty SpacingProperty=
BindableProperty.Create
(
“间距”,
类型(双),
类型(播放),
10.0,
propertyChanged:(可绑定、旧值、新值)=>((WrapLayout)可绑定)
);
}
您是在C#模式下设置ItemsSource属性还是使用绑定?“我对你的问题文本感到有点困惑。@Diegrafaelsouza抱歉,我对Xamarin是相当陌生的,但我以为我在我的C#代码后面设置了绑定。我不确定这是否与您的问题相关,但我没有使用MVVM-我仅有的两个文件是ListPage.xaml和ListPage.xaml.cs。在我看来,在没有MVVM的情况下使用绑定太难了,但这是很有可能的。您必须将源作为公共属性。您的c#代码必须看起来像public observeCollection inactiveItems{get;set;}=new observeCollection(App.ListItemRepo.GetListItems())代码>然后inactiveList.SetBinding(WrapLayout.ItemsSourceProperty,新绑定(“inactiveItems”))ItemsSource=inactiveItems;inactiveList.BindingContext=这个代码>。是你问的吗?(为我糟糕的英语感到抱歉)@diegrafaelsouza我得到了“在当前上下文中不存在'inactiveList'这个名称”。我应该添加一些参考资料吗?让我们在聊天中讨论一下: