Leo*_*Leo 5 c# java android xamarin
在android java中,如果我想从非原始线程使用我的视图,我这样写:
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
String text = (String) msg.obj;
myTextView.setText(text);
}
};
Run Code Online (Sandbox Code Playgroud)
一切正常.但在xamarin C#中我写道:
Handler h = new Handler()
{
public override void HandleMessage (Message msg)
{
}
};
Run Code Online (Sandbox Code Playgroud)
并看到 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");
});
Run Code Online (Sandbox Code Playgroud)
您不能覆盖对象初始值设定项内的方法。您必须声明一个继承Handler并重写的类HandleMessage:
public class MyHandler : Handler
{
public override void HandleMessage (Message msg)
{
// Some stuff
}
}
Run Code Online (Sandbox Code Playgroud)
来自MSDN:
匿名类型包含一个或多个公共只读属性。其他类型的类成员(例如方法或事件)都是有效的。用于初始化属性的表达式不能为 null、匿名函数或指针类型。
因此,匿名类型只能包含公共属性。不是方法。