2015-02-11 88 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)); 
    }