MonoTouch.Dialog:元素中文本的更新
使用MonoTouch.Dialog
我添加了StyledStringElement
元素。
有一个后台任务可以检索需要更新element.Value
详细信息
有没有办法强制元素在element.Value
更新后更新文本?
伊恩
如果您想逐个元素更新,那么您可以使用以下内容:
public class MyStyledStringElement { public void SetValueAndUpdate (string value) { Value = value; if (GetContainerTableView () != null) { var root = GetImmediateRootElement (); root.Reload (this, UITableViewRowAnimation.Fade); } } }
一种变化是加载所有内容并更新一次(即迭代每个Element
的root.Reload
)。
我已经将“this.PrepareCell(cell);添加到SetValueAndUpdate方法并且可以正常工作。但我仍然认为还有另一个更好的选择来检测”caption“的变化并调用this.PrepareCell(cell);.
public void UpdateCaption(string caption) { this.Caption = caption; Console.WriteLine ("actualizando : " + Caption); UITableViewCell cell = this.GetActiveCell (); if (cell != null) { this.PrepareCell (cell); cell.SetNeedsLayout (); } }
更新标签的另一种方法是从StyledStringElement
或StringElement
派生,并直接刷新单元格中的DetailTextLabel:
class UpdateableStringElement : StringElement { public UpdateableStringElement(string name): base (name) { } UILabel DetailText = null; public void UpdateValue(string text) { Value = text; if (DetailText != null) DetailText.Text = text; } public override UITableViewCell GetCell(UITableView tv) { var cell = base.GetCell(tv); DetailText = cell.DetailTextLabel; return cell; } }
您可以使用UpdateValue
方法代替Value
属性:
var element = new UpdateableStringElement("demo"); SomeEventOfYours += delegate { element.UpdateValue(LocalizedValue); };