我创建了一个视图,显示弹出当前视图的用户丢失的连接消息。我想根据连接状态的变化定期更新视图。
我可以正确地获取视图并更改标签的文本(使用WriteLines验证),但实际显示没有任何变化。我甚至尝试删除视图并读取它并调用SetNeedsDisplay,但似乎没有任何帮助。
我有一个名为OverView的全局变量:
public static UIView OverView;
我创建标签子视图,将其添加到概述中并弹出当前视图前面的概述:
UILabel labelTitle = new UILabel();
labelTitle.Text = title;
UIView labelTitleView = (UIView) labelTitle;
labelTitleView.Tag = 5000;
OverView.AddSubview(labelTitleView);
curView.InsertSubviewAbove(OverView, curView);
curView.BringSubviewToFront(OverView);
然后稍后,我尝试从另一个函数中修改它:
if ((OverView != null) && (OverView.Subviews != null))
{
for (int i = 0; i < OverView.Subviews.Length; i++)
{
WriteToConsole("Type: " + OverView.Subviews[i].GetType());
if (OverView.Subviews[i] is UILabel)
{
WriteToConsole("Found Label with Tag: " + ((UILabel)(OverView.Subviews[i])).Tag + " Text: " + ((UILabel)(OverView.Subviews[i])).Text);
if (((UILabel)(OverView.Subviews[i])).Tag == 5000)
{
WriteToConsole("Setting subview Title to: " + lostConnectionTitle);
lock (overViewLocker)
{
appReference.InvokeOnMainThread(delegate
{
UILabel tempLabel = ((UILabel)(OverView.Subviews[i]));
tempLabel.Text = lostConnectionTitle;
OverView.Subviews[i].RemoveFromSuperview();
OverView.AddSubview(tempLabel);
OverView.BringSubviewToFront(tempLabel);
OverView.SetNeedsLayout();
OverView.SetNeedsDisplay();
WriteToConsole("SetNeedsDisplay");
});
}
}
}
}
}
答案 0 :(得分:0)
您是否尝试在标签上使用委托方法,并在事件发生时更改其值?
例如,如果您的活动点击某个按钮,则应该具有以下内容:
yourLabel.Text = "Init";
buttonExample.TouchUpInside += (sender, e) => {
yourLabel.Text = "I touched my button";
};
当你的视图加载时,你会看到“Init”和你的按钮,一旦你点击它,标签文字就会改变。
Xamarin对事件和委托方法here有一些解释。
我希望有所帮助。