在android java中,如果我想从非原始线程使用我的视图,那么我写道:
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
String text = (String) msg.obj;
myTextView.setText(text);
}
};
一切正常。但在xamarin C#中我写道:
Handler h = new Handler()
{
public override void HandleMessage (Message msg)
{
}
};
并查看invalid initializer member declarator
如何重新加载HandleMessage
方法?我可以用其他方式从另一个线程中使用我的视图吗?
编辑:
@AntP,这种方式在xamarin中不起作用:Only the original thread that created a view hierarchy can touch its views.
但感谢您的支持。
解决方案:
mActivity.RunOnUiThread(delegate
{
mTextView.Text = ("Test");
});
答案 0 :(得分:3)
您无法覆盖对象初始值设定项内的方法。您必须声明一个继承Handler
并覆盖HandleMessage
的类:
public class MyHandler : Handler
{
public override void HandleMessage (Message msg)
{
// Some stuff
}
}
来自MSDN:
匿名类型包含一个或多个公共只读属性。没有 其他类成员,,如方法或事件,都是有效的。 用于初始化属性的表达式不能为null, 匿名函数或指针类型。
因此,匿名类型只能包含公共属性。不是方法。