正在寻找解释 System.NotSupportedException

Looking for explaination System.NotSupportedException

所以首先是一些代码,最后是问题。 我有一些叫做机器的对象,它们有两个属性

public Logs MachineLogs {
        get { return _logs; }
        set {
            _logs = value;
            NotifyPropertyChanged ("MachineLogs");
        }
    }

    public ObservableCollection<Part> Parts {
        get { return _parts; }
        set {
            _parts = value;
            NotifyPropertyChanged ("Parts");
        }
    }

MachineLogs 看起来像这样:

public ObservableCollection<Log> Malfunctions {
        get {
            SortCollection (_malfunctions);
            return _malfunctions;
        }
        set {
            _malfunctions = value;
            NotifyPropertyChanged ("Malfunctions");
        }
    }

    public ObservableCollection<Log> CompletedTasks {
        get {
            SortCollection (_completedTasks);

            return _completedTasks;
        }
        set {
            _completedTasks = value;
            NotifyPropertyChanged ("CompletedTasks");
        }
    }

    public ObservableCollection<LogTemplate> TaskTemplates {
        get { return _taskTemplates; }
        set {
            _taskTemplates = value;
            NotifyPropertyChanged ("TaskTemplates");
        }
    }

现在,我在 BackgroundWorker 中使用序列化克隆 Machine,然后将其添加到我存储它的映射中。

protected override void CloneReady ( object sender, RunWorkerCompletedEventArgs e )
    {
        var machine = ((Machine) e.Result);
        _map.Machines.Add (machine);

        var machineControl = new MachineControl (machine, _canvas);
        ((MainWindow) Application.Current.MainWindow).MapPanel.Selector.ProcessSelection (machineControl);

    }

问题来了。当我向其中添加项目或删除项目时,部件集合一切正常(两个集合的方法看起来完全相同)。当我试图对故障集合执行任何操作时发生异常。

This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread.



private void AddNewEntry(object sender, RoutedEventArgs e) {
        if (LogList.SelectedLog == null)
        {
            var source = (ObservableCollection<Log>) LogList.ItemsSource;

            Log newLog = new Log (LogTypes.Malfunction, ((Machine) DataContext).MachineLogs.Colors.MalfunctionColor.HexValue)
            {
                DbAction = Data.DBSavers.DatabaseActions.Create,
                MachineId = ((Machine) DataContext).Db.Id
            };
            source.Insert (0, newLog);
            LogList.SelectedLog = newLog;
        }
        else
        {
            LogList.SelectedLog = null;
        }
    }

AddNewEntry 由 UI 按钮调用,尝试调用 Dispatcher 但仍然没有成功。对这种行为有什么解释吗? 当我跳过 BackgroundWorker 部分时,不会出现问题。我不能跳过它的原因是我需要克隆多台机器(copy/paste 东西)并且它可能需要一段时间所以我不想冻结 UI.

protected override void ProduceClone ( object sender, DoWorkEventArgs e )
    {
        var serializer   = new XmlSerializer ();
        var selector     = new MapColorSelector ();
        var machine      = serializer.SerializeMachine (e.Argument as Machine);
        machine.Color    = selector.DefaultColor;
        machine.Location = _location;

        e.Result = machine;
    }

我喜欢这个网站帮助解决问题的方式。大多数答案都是在描述问题并仔细研究之后得出的。 这次是排序方法的错。去了 LINQ,它再次工作,但也许有人可以解释一下:]

private void SortCollection (ObservableCollection<Log> collection) {
        var sorted = CollectionViewSource.GetDefaultView (collection);
        sorted.SortDescriptions.Add (new SortDescription ("Date", ListSortDirection.Ascending));
    }