在 Terminal.GUI (gui.cs) 中更改 ListView 后刷新 UI 的正确方法是什么?

What is the correct way to refresh the UI after changing a ListView in Terminal.GUI (gui.cs)?

我正在使用 gui.cs

我有一个显示网络节点的 ListView。这些节点来来往往,因此列表会根据正确的事件进行更新。

var clients = new List<Node>();
var clientList = new ListView(clients)
{
    Height = Dim.Fill(),
    Width = Dim.Fill(),
};

server.NodeJoined += (s, e) =>
{
    clients.Add(e.Node);
    Application.Refresh();
};

server.NodeLeft += (s, e) =>
{
    var client = clients.FirstOrDefault(n => n.IP == e.Node.IP);
    if (client != null) clients.Remove(client);
    Application.Refresh();
};

目前我正在使用更新整个 UI 的 Application.Refresh()。理想情况下,应该只更新更改的部分。这是正确的还是有更好的方法来通知 ListView 数据源已更改并且需要重绘?

从回调中进行 UI 更改的正确方法是在您的视图上使用 MainLoop.Invoke. If you do that then you can simply call SetNeedsDisplay

Invoke 确保在呈现视图时不会发生列表内容更改,这意味着会立即检测到 SetNeedsDisplay 并发生重绘。

ListView clientList = new ListView();
Application.MainLoop.Invoke(()=>
{
    // TODO: Change list contents here

    // Tell view to redraw
    clientList.SetNeedsDisplay();
});