2

使用MonoTouch.Dialog我添加StyledStringElement元素。

有一个后台任务检索需要更新的详细信息element.Value

有没有办法强制元素在更新后element.Value更新它的文本?

伊恩

4

3 回答 3

12

如果要逐个元素更新此元素,则可以使用以下内容:

public class MyStyledStringElement {

    public void SetValueAndUpdate (string value)
    {
        Value = value;
        if (GetContainerTableView () != null) {
            var root = GetImmediateRootElement ();
            root.Reload (this, UITableViewRowAnimation.Fade);
        }
    }
}

一种变体是加载所有内容并更新一次(即在root.Reloadfor each 上迭代Element)。

于 2011-12-13T22:16:56.197 回答
1

我已经在 SetValueAndUpdate 方法中添加了“this.PrepareCell (cell); 并且可以工作。但我仍然认为还有另一个更好的选择来检测“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 ();
        }
}
于 2013-05-27T20:13:46.250 回答
1

另一种更新标签的方法是从单元格中的 DetailTextLabel派生StyledStringElement或直接刷新:StringElement

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;
    }
}

Value然后,您可以使用以下UpdateValue方法代替属性:

var element = new UpdateableStringElement("demo");

SomeEventOfYours += delegate {
    element.UpdateValue(LocalizedValue);
};
于 2013-09-25T10:50:47.803 回答