C# 更改ListCell中的Xamarin标签无效
我对ListView有问题。我希望每个单元格都有一个标签和一个开关,但标签的文本不会出现 这是我的密码:C# 更改ListCell中的Xamarin标签无效,c#,xamarin,xamarin.forms,C#,Xamarin,Xamarin.forms,我对ListView有问题。我希望每个单元格都有一个标签和一个开关,但标签的文本不会出现 这是我的密码: public class FilterPage : ContentPage { public FilterPage() { List<FilterCell> listContent = new List<FilterCell>(); foreach(string type in Database.RestaurantTy
public class FilterPage : ContentPage
{
public FilterPage()
{
List<FilterCell> listContent = new List<FilterCell>();
foreach(string type in Database.RestaurantTypes)
{
FilterCell fc = new FilterCell();
fc.Text = type;
listContent.Add(fc);
}
ListView types = new ListView();
types.ItemTemplate = new DataTemplate(typeof(FilterCell));
types.ItemsSource = listContent;
var layout = new StackLayout();
layout.Children.Add(types);
Content = layout;
}
}
public class FilterCell : ViewCell
{
private Label label;
public Switch CellSwitch { get; private set; }
public String Text{ get { return label.Text; } set { label.Text = value; } }
public FilterCell()
{
label = new Label();
CellSwitch = new Switch();
var layout = new StackLayout
{
Padding = new Thickness(20, 0, 0, 0),
Orientation = StackOrientation.Horizontal,
HorizontalOptions = LayoutOptions.FillAndExpand,
Children = { label, CellSwitch }
};
View = layout;
}
}
public类过滤器页面:ContentPage
{
公共过滤器页()
{
List listContent=新列表();
foreach(数据库中的字符串类型。RestaurantTypes)
{
FilterCell fc=新的FilterCell();
fc.Text=类型;
listContent.Add(fc);
}
ListView类型=新建ListView();
types.ItemTemplate=新数据模板(typeof(FilterCell));
types.ItemsSource=listContent;
var布局=新的StackLayout();
布局。子项。添加(类型);
内容=布局;
}
}
公共类筛选器单元格:ViewCell
{
自有品牌;
公共交换机CellSwitch{get;private set;}
公共字符串Text{get{return label.Text;}set{label.Text=value;}}
公共过滤器单元()
{
标签=新标签();
CellSwitch=新开关();
var布局=新的StackLayout
{
填充=新厚度(20,0,0,0),
方向=堆叠方向。水平,
HorizontalOptions=LayoutOptions.FillAndExpand,
子项={label,CellSwitch}
};
视图=布局;
}
}
如果我在FilterCell构造函数中输入固定文本,它可以正常工作(例如:label.Text=“Hello World”)
当我为ItemSelected事件创建一个方法并读取SelectedItem.Text属性时,我得到了指定为值的文本,但它从未显示。当我尝试运行此代码时,仅显示开关
谢谢你的帮助
妮可哦,男孩。这段代码看起来像强奸(很抱歉我不得不这么说) 现在让我们看看有什么问题: 原因是您将数据和视图混为一谈 线路
types.ItemTemplate = new DataTemplate(typeof(FilterCell));
表示:“为列表中的每个项目(ItemsSource
)创建一个新的筛选单元格”。在循环中创建的FilterCell
s将永远不会显示
轻松修复
public class FilterPage : ContentPage
{
public FilterPage()
{
var restaurantTypes = new[] {"Pizza", "China", "German"}; // Database.RestaurantTypes
ListView types = new ListView();
types.ItemTemplate = new DataTemplate(() =>
{
var cell = new SwitchCell();
cell.SetBinding(SwitchCell.TextProperty, ".");
return cell;
});
types.ItemsSource = restaurantTypes;
Content = types;
}
}
- 有一种标准单元格类型,它包含一个标签和一个开关
,请使用它SwitchCell
- 作为列表的
,您必须使用数据。在你的例子中,餐馆类型的列表。我只是用一个静态列表来嘲弄他们ItemsSource
- DataTemplate创建
,并设置文本属性的数据绑定。这是视图和数据之间的神奇粘合剂。“.”将其绑定到数据项本身。我们使用它,因为我们的列表包含字符串项,并且文本应该正好是字符串。(阅读有关数据绑定的内容:)开关单元
- 我将包含列表的
剥离掉。您可以直接将列表设置为页面的StackLayout
Content
- 如果可能,使用标准控件
- 您应该始终记住将数据和视图彼此分开,并使用数据绑定彼此连接
- 尽量避免不必要的意见
- 哦,孩子。这段代码看起来像强奸(很抱歉我不得不这么说)
现在让我们看看有什么问题:
原因是您将数据和视图混为一谈
线路
types.ItemTemplate = new DataTemplate(typeof(FilterCell));
表示:“为列表中的每个项目(ItemsSource
)创建一个新的筛选单元格”。在循环中创建的FilterCell
s将永远不会显示
轻松修复
public class FilterPage : ContentPage
{
public FilterPage()
{
var restaurantTypes = new[] {"Pizza", "China", "German"}; // Database.RestaurantTypes
ListView types = new ListView();
types.ItemTemplate = new DataTemplate(() =>
{
var cell = new SwitchCell();
cell.SetBinding(SwitchCell.TextProperty, ".");
return cell;
});
types.ItemsSource = restaurantTypes;
Content = types;
}
}
- 有一种标准单元格类型,它包含一个标签和一个开关
,请使用它SwitchCell
- 作为列表的
,您必须使用数据。在你的例子中,餐馆类型的列表。我只是用一个静态列表来嘲弄他们ItemsSource
- DataTemplate创建
,并设置文本属性的数据绑定。这是视图和数据之间的神奇粘合剂。“.”将其绑定到数据项本身。我们使用它,因为我们的列表包含字符串项,并且文本应该正好是字符串。(阅读有关数据绑定的内容:)开关单元
- 我将包含列表的
剥离掉。您可以直接将列表设置为页面的StackLayout
Content
- 如果可能,使用标准控件
- 您应该始终记住将数据和视图彼此分开,并使用数据绑定彼此连接
- 尽量避免不必要的意见
字符串名
和bool启用
的结构的详细信息。不,我无法将这些属性绑定到SwitchCell。作为ItemSource,我传递了这些结构的列表。cell.SetBinding(Switc