加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C#:初始化时覆盖方法

发布时间:2020-12-15 05:39:30 所属栏目:百科 来源:网络整理
导读:在 android java中,如果我想从非原始线程使用我的视图,我这样写: Handler handler = new Handler() { @Override public void handleMessage(Message msg) { String text = (String) msg.obj; myTextView.setText(text); }}; 一切正常.但在xamarin C#中我写
在 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)
    {

    }
};

并看到无效的初始化成员声明符

如何重新加载HandleMessage方法?我可以用其他方式从另一个线程中使用我的视图吗?

编辑:
@AntP,这种方式在xamarin中不起作用:只有创建视图层次结构的原始线程才能触及其视图.但感谢您的支持.

解:

mActivity.RunOnUiThread(delegate
{
    mTextView.Text = ("Test");
});

解决方法

您不能覆盖对象初始值设定项内的方法.您必须声明一个继承Handler并重写HandleMessage的类:
public class MyHandler : Handler
{
    public override void HandleMessage (Message msg)
    {
        // Some stuff
    }
}

从MSDN开始:

Anonymous types contain one or more public read-only properties. No
other kinds of class members,such as methods or events,are valid.
The expression that is used to initialize a property cannot be null,
an anonymous function,or a pointer type.

因此,匿名类型只能包含公共属性.不是方法.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读