最近研究 .Net Core 3.1 WPF 开发
发现在WPF中常用的Dispatcher.BeginInvoke 更新UI界面失效了
线程也是同一个线程,太奇怪了
这边提供两种解法
第一种
var scheduler = TaskScheduler.FromCurrentSynchronizationContext();
Task.Factory.StartNew(()=> {}).ContinueWith(r=>
{
ControlItem.Dispatcher.BeginInvoke((System.Action)(delegate
{
/* UI更新操作*/
}));
}, scheduler);
第二种
使用委托
public void UpdateName(string username)
{
this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new UIDelegate(UpdateNameForUI), username);
}
public delegate void UIDelegate(string username);
public void UpdateNameForUI(string username)
{
/*更新UI*/
}
后续在研究下,看能不能有其他更好的解法