我希望有一个页面具有仅输入单元格的表格视图。这是应用程序内部的“更新详细信息”功能。我想使其更加用户友好,因为我将输入单元格分为两部分,并用对象中已有的数据填充顶部部分,而我放置了一个占位符,这是空值。
到目前为止,我到处都只有if和else。我检查每个属性,看看是否为空。如果没有,我将其值放在输入单元格中。如果是这样,我将添加一个占位符。
if (car.Make != null)
Weknow.Add(new EntryCell {Label = "Make", Text = car.Make});
else
Wedont.Add(new EntryCell {Label = "Make", Placeholder = "Eg. Ford"});
if (car.Manufacturer != null)
Weknow.Add(new EntryCell {Label = "Manufacturer", Text = car.Manufacturer});
else
Wedont.Add(new EntryCell {Label = "Manufacturer", Placeholder = "Eg. Ford Werke AG"});
if (car.Plant != null)
Weknow.Add(new EntryCell {Label = "Plant", Text = car.Plant});
else
Wedont.Add(new EntryCell {Label = "Plant", Placeholder = "Eg. Koeln-Niehl"});
if (car.ModelYear != null)
Weknow.Add(new EntryCell {Label = "ModelYear", Text = car.ModelYear});
else
Wedont.Add(new EntryCell {Label = "ModelYear", Placeholder = "Eg. 2010"});
if (car.SequentialNumber != null)
Weknow.Add(new EntryCell {Label = "SequentialNumber", Text = car.SequentialNumber});
else
Wedont.Add(new EntryCell {Label = "SequentialNumber", Placeholder = ""});
if (car.Model != null)
Weknow.Add(new EntryCell {Label = "Model", Text = car.Model});
else
Wedont.Add(new EntryCell {Label = "Model", Placeholder = "Eg. Focus"});
Weknow是包含具有值的Entry单元格的表部分,而Wedont是具有空值并具有占位符的部分。
最终结果是这样的: https://imgur.com/pr2ezQr
一切正常,但是我想知道是否有任何更简单的方法来遍历对象并查看属性是否为null,如果有的话,那么应该有一个占位符。我当时想拥有一个绑定上下文,但是我想用代码而不是XAML来完成所有这些工作。但是如果可以那样做,我可以尝试。
谢谢!
答案 0 :(得分:0)
Placeholder Property在Xamarin表单中的工作方式仅在Text属性为null或为空时显示。如果Text不为null或为空,则将显示Text,而不会显示Placeholder。鉴于此,您可以将呼叫更新为:
Weknow.Add(new EntryCell {Label = "Make", Placeholder = "Eg. Ford", Text = car.Make});
Weknow.Add(new EntryCell {Label = "Manufacturer", Placeholder = "Eg. Ford Werke AG", Text = car.Manufacturer});
Weknow.Add(new EntryCell {Label = "Plant", Placeholder = "Eg. Koeln-Niehl", Text = car.Plant});
// Continue with other cells.
这使您可以删除条件语句。