Xamarin Handler.Post 不调用 IRunnable.Run()

Xamarin Handler.Post does not invoke IRunnable.Run()

我正在尝试 运行 Xamarin 中的这个 Android 代码:

final Handler handler = new Handler();
handler.post(new Runnable() {
    @Override
    public void run() {
    int hours = seconds/3600;
    int minutes = (seconds%3600)/60;
    int secs = seconds%60;

    String time = String.format("%d:%02d:%02d", hours, minutes, secs);
    timeView.setText(time);
    if (running) {
        seconds++;
    }
    handler.postDelayed(this, 1000);
    }
});

我已经在 Xamarin 中尝试过了;

Test newTest = new Test(this);
Handler handler = new Handler();
handler.Post(newTest);

测试 class 是 IRunnable 作为

public class Test : IRunnable
{
    MainActivity activity;
    public Test(MainActivity test)
    {
        activity = test;
    }

    public IntPtr Handle
    {
        get
        {
            return (IntPtr)0;
        }
    }

    public void Dispose()
    {

    }

    public void Run()
    {
        Console.WriteLine("-------------------->Run called<-----------------------");
        TextView stopWatch = activity.getTextView();
        int hour = activity.seconds / 3600;
        int minute = (activity.seconds % 3600) / 60;
        int sec = activity.seconds % 60;

        string time = string.Format("%d:%02d:%02d", hour, minute, sec);
        stopWatch.Text = time;

        if (activity.running == true)
        {
            activity.seconds++;
        }

        Handler handler = new Handler();
        handler.PostDelayed(this, 1000);
    }
}

程序 运行s 但 Handler 没有调用 Run() 函数。我是 Xamarin 的新手。有什么解决办法吗?

这是一个典型的错误。 提示隐藏在构建输出中。

Type 'App1.Test' implements Android.Runtime.IJavaObject but does not inherit from Java.Lang.Object. It is not supported.

每次,你实现一个Java接口,你必须继承自Java.Lang.Object

public class Test : Java.Lang.Object, IRunnable
{
    // ...
}