2015-02-11 90 views
-1

所以首先一些代码,问题到底。 我有一些物体被称为机器,他们两个属性寻找解释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的,然后将其添加到地图中,我存储它。

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); 

    } 

现在是问题所在。一切工作正常与部分收集,当我添加项目或删除(两个集合的方法看起来完全相同)。当我尝试对Malfunctions集合执行任何操作时发生异常。

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按钮调用,尝试调用调度程序但仍然没有运气。有没有解释这种行为? 当我使用BackgroundWorker跳过部件时,问题不会发生。为什么我不能跳过它的原因是我需要克隆多台机器(复制/粘贴东西),它可能需要一段时间,所以我不想冻结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; 
    } 
+1

我可以得到一些评论这些弊端?很高兴知道我在代码中做了什么错误,从中学到了一些东西。 – yoger 2015-02-12 06:20:07

回答

0

我喜欢这个网站如何帮助解决问题。大部分答案都是在描述问题并仔细研究之后才提出的。 这次是排序方法的错。去了LINQ和它再次运作,但也许有人可以解释这个吗:]

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