我目前正在将我的Android应用程序的java代码迁移到C#。我想在线程执行的中间更新我的UI。
这是我的java代码: -
private Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
if (msg.what == MSG_SURFACE_CREATED) {
contentWidth = 0;
contentHeight = 0;
requestLayout();
return true;
} else {
Log.w("Unknown msg.what: " + msg.what);
}
return false;
}
});
和: -
void postChangedToView(final int indexInAdapter) {
handler.post(new Runnable() {
@Override
public void run() {
changedToView(indexInAdapter, true);
}
});
}
我在c#中尝试过类似的东西: -
private Android.OS.Handler handler = new Android.OS.Handler();
private class Callback : Android.OS.Handler.ICallback //inner class
{
ViewController fp; //Create instance of outer class
public Callback(FViewController _fp) //pass the instance to constructor of inner class
{
fp = _fp;
}
#region ICallback implementation
public bool HandleMessage (Message msg)
{
if (msg.What == MSG_SURFACE_CREATED)
{
contentWidth = 0;
contentHeight = 0;
fp.RequestLayout ();
return true;
}
else
{
Log.w("Unknown msg.what: " + msg.What);
}
return false;
throw new NotImplementedException ();
}
}
这里我不能创建一个内联类Handler.ICallBack
和: -
internal virtual void postChangedToView(int indexInAdapter) {
handler.Post (Task.Run (()=> flippedToView (indexInAdapter,true)));
}
我在这里得到一个错误说: -
Error CS1503: Argument 1: cannot convert from 'System.Threading.Tasks.Task' to 'System.Action'
答案 0 :(得分:1)
Handler.Post
需要System.Action
个参数。您可以创建System.Action
,如下所示:
internal virtual void postFlippedToView(int indexInAdapter)
{
Action action = () => flippedToView(indexInAdapter, true);
handler.Post (action );
}