如何在Xamarin Android中使用RunOnUIThread()

Sur*_*raj 9 xamarin xamarin-studio

我想在将结果输入OStateUserId对象后运行此语句.

details.Text = OStateUserID.name + "\n" + OStateUserID.population);

bStateuserID.Click +=  async delegate 
{
    details.Text=" ";
    Guid x = new Guid("33F8A8D5-0DF2-47A0-9C79-002351A95F88");
    state OStateUserID =(state) await obj.GetStateByUserId(x.ToString());
    details.Text = OStateUserID.name + "\n" + OStateUserID.population);
};
Run Code Online (Sandbox Code Playgroud)

GetStateByUserId()方法返回一个对象class State.

它以异步方式运行.完成操作后,我想将名称填充分配给TextView详细信息.

RunOnUIThread()在这种情况下如何使用?

For*_*gic 15

我不确定你怎么能错过它,因为你有正确的功能名称.无论如何,你可以使用lambda表达式:

Activity.RunOnUiThread(() => {
    details.Text = OStateUserID.name + "\n" + OStateUserID.population;
});
Run Code Online (Sandbox Code Playgroud)

或者你可以为它创建一个方法

Activity.RunOnUiThread(Action);

private void Action() 
{ 
    details.Text = OStateUserID.name + "\n" + OStateUserID.population;
}
Run Code Online (Sandbox Code Playgroud)

在后一种情况下,如果不是这种情况,则必须将变量存储在私有字段中.在第一种情况下,如果变量与RunOnUiThread调用的范围相同,它将起作用.


Ste*_*ary 6

不用了

在此代码中,您发布了:

bStateuserID.Click +=  async delegate 
{
    details.Text=" ";
    Guid x = new Guid("33F8A8D5-0DF2-47A0-9C79-002351A95F88");
    state OStateUserID =(state) await obj.GetStateByUserId(x.ToString());
    details.Text = OStateUserID.name + "\n" + OStateUserID.population);
};
Run Code Online (Sandbox Code Playgroud)

线

details.Text = OStateUserID.name + "\n" + OStateUserID.population);
Run Code Online (Sandbox Code Playgroud)

已在UI线程上运行。

我在博客上有一个async介绍此行为的介绍。