Uno 平台:从异步线程调用 UI 更新

Uno platform: invoke UI update from async thread

我有一个 async 成员,在从服务器获取一些数据后,它最终需要调用一些 UI 更新。

我想我需要 BeginInvokeOnMainThreadDispatcher.Invoke 之类的东西,但在 Uno 上下文中似乎无法识别这些东西。

这是我的精华:

public async Task LoadList()
{
  ...
  // get data
  Uri uri = new Uri("https://...");
  response = await httpClient.GetAsync(uri);

  // display
  BeginInvokeOnMainThread () =>
  {
    ... update the UI ...
  });
}

但是我收到错误 CS0103 The name 'BeginInvokeOnMainThread' does not exist in the current context UnoTest.Droid, UnoTest.UWP, UnoTest.Wasm

BeginInvokeOnMainThread / Dispatcher.Invoke / Control.Invoke 从来都不是个好主意。

Uno 应该有一个 SynchronizationContext,即 automatically used by await,因此不需要手动线程编组:

public async Task LoadList()
{
  ...
  // get data
  Uri uri = new Uri("https://...");
  response = await httpClient.GetAsync(uri);

  // display
  ... update the UI ...
}