Dispatcher.CurrentDispatcher
获取当前执行的线程的调度程序,如果尚未与该线程关联,则创建新的调度程序。
您应该使用当前应用程序实例的调度程序:
ThreadPool.QueueUserWorkItem(o =>
{
var result = getDataFromDatabase();
Application.Current.Dispatcher.Invoke(() =>
{
LstUsers = result;
IsSplashVisible = false;
});
});
Dispatcher.CurrentDispatcher
在UI线程中而不是在线程池线程中调用。然而,该字段是完全冗余的。你可以随时打电话
Application.Current.Dispatcher.Invoke()
public class TestViewModel
{
private readonly Dispatcher _dispatcher = Dispatcher.CurrentDispatcher;
public TestViewModel()
{
IsSplashVisible = true;
ThreadPool.QueueUserWorkItem(o =>
{
var result = getDataFromDatabase();
_dispatcher.Invoke(() =>
{
LstUsers = result;
IsSplashVisible = false;
});
});
}
...
}