我想在单独的线程中触发添加项目事件,UI部分可以更新数据

时间:2019-05-30 19:05:38

标签: c# wpf mvvm

我拥有它们:LogView / LogViewModel / LogService(静态) 在UI线程上,LogViewModel触发添加项事件,新数据可以真正在LogView上更新 但是在单独的线程上,LogView上没有更新,例如在套接字线程上。

LogService:

public delegate void NewLogEventHandler(object sender, NewLogEventArgs e);
  public static class LogService
  {
    public static event NewLogEventHandler NewLogEvent;
  }
}

LogViewModel:

private List<LogItemViewModel> logItems;
public List<LogItemViewModel> LogViewItems {
  get { return logItems; }
  set {
  logItems = value;
      this.RaisePropertyChanged("LogViewItems");
  }
}
public LogViewMode(){
  LogService.NewLogEvent += LogServiceNewLogEvent;
}

在任何线程中,每次添加后都可以更新并显示日志视图中的项目

但是实际上,只能更新UI线程。

0 个答案:

没有答案