NotSupportedException 未处理 - 但为什么呢?

NotSupportedException was unhandled - but why?

我收到异常,但我不明白为什么会收到此消息。 我想用 SQL.

中的数据更新 DataGrid

问题似乎与我的 ObservableCollection..

有关

代码如下:

private void Refresh()
{
    new Thread(() =>
    {
        List<DatabaseEntry> entries = GetBrokenJobs();
        Dispatcher.CurrentDispatcher.Invoke(() =>
        {
           UpdateUi(entries);
        });
    }).Start();
}

private void UpdateUi(List<DatabaseEntry> entries)
{
    Jobs.Clear();//<== !!! Exception origin !!!
    foreach (var jobName in entries.SelectMany(x => x.JobBezeichnung as string).ToList())
    {
       Jobs.Add(jobName.ToString());
    }
}

异常是说当我不在 Dispatcher-Thread 上时我无法修改 "SourceCollection"...

但是我正在调用 Dispatcher 上的调用方法?!

我在这里错过了什么?错在哪里?

我通过在我的 ViewModel 中创建一个静态变量来修复它:

public static Dispatcher UI_Dispatcher { get; set; }

然后在我的 ViewModel 中:

public MainViewModel(Main window)
{
    UI_Dispatcher = Dispatcher.CurrentDispatcher; //Since we are on the UI-Thread when passing this piece of code this is the right Dispatcher
}

后来我只是在 UI_Dispatcher.Invoke() 上调用了。

您可以 运行 在 UI 讨论帖中这样

 Application.Current.Dispatcher.Invoke(() =>
 {
    UpdateUi(entries);
 });