我找不到正确的NuGet包来安装invoke方法。我有一个wpf GUI,我有一个单独的线程,它需要更新listBox中的项。我需要"Invoke“方法来更改listBox中的项。
public void displayPlayers(string players)
{
//spliting all the names.
string[] names = players.Split(", ".ToCharArray());
//Displaying the names.
foreach (string name in names)
this.Invoke((MethodInvoker)(() => playersListBox.Items.Add(name)));
}发布于 2019-07-02 18:28:54
使用Dispatcher.Invoke()方法。它可以通过Application类(see more)访问,也可以通过控件本身访问。有关更多信息,请参阅:Dispatcher.Invoke
发布于 2019-07-02 18:33:00
这对我来说很有效:
await System.Windows.Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.ContextIdle, new Action(() =>
{
playersListBox.Items.Add(name);
}));或者不需要等待:
System.Windows.Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.ContextIdle, new Action(() =>
{
playersListBox.Items.Add(name);
})).Wait();https://stackoverflow.com/questions/56849968
复制相似问题