【问题标题】:How to use the RunOnUiThread on ContentPage on Xamarin Forms如何在 Xamarin 表单的 ContentPage 上使用 RunOnUiThread
【发布时间】:2018-12-17 15:56:41
【问题描述】:

我想使用 RunOnUiThread 通过 Timer 使用 UI 线程更新 UI(图表和标签)。我正在使用内容页面。如何在 Content Page 上使用为 RunOnUiThread 添加 Activity 类?不支持。

我想做以下事情:

RunOnUiThread(() => { tvTimer.Text = Convert.ToString(CountSeconds );});

目前我正在使用

Device.BeginInvokeOnMainThread(async () => });

用于更新 UI,但 UI 卡住或无法正确更新,例如每秒显示倒数计时器。

请帮帮我

【问题讨论】:

  • 请出示相关代码
  • 添加大约 100 毫秒的延迟,然后尝试 Device.BeginInvokeOnMainThread(async () => });
  • 您不能在 Xamarin 表单中使用 RunOnUiThread。这是 Xamarin.Android 中的一个函数。您可以显示更多相关代码来描述您的问题。

标签: multithreading xaml xamarin.forms async-await


【解决方案1】:

我认为发生这种情况的原因是因为您可能犯了一些小错误,而从我看到的代码中我无法判断出这一点,所以我将提供一个参考类的东西供您查看:

定义要在主线程上执行的方法:

 private async void MethodOnUIThread()
    {
       await **something**;
       //Probably your timer code 
       //This method does not necessarily have to be async.
    }

在主线程上运行类似这样的方法:

   Device.BeginInvokeOnMainThread(MethodOnUIThread);

如有疑问或不明白的地方,请回复

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-16
    相关资源
    最近更新 更多