gpt4 book ai didi

c# - 这种类型的 CollectionView 不支持从不同于 Dispatcher 线程的线程更改其 SourceCollection

转载 作者:IT王子 更新时间:2023-10-29 03:33:33 60 4
gpt4 key购买 nike

<分区>

我有一个 DataGrid,它通过异步方法从 ViewModel 填充数据。我的 DataGrid 是:

<DataGrid ItemsSource="{Binding MatchObsCollection}"  x:Name="dataGridParent" 
Style="{StaticResource EfesDataGridStyle}"
HorizontalGridLinesBrush="#DADADA" VerticalGridLinesBrush="#DADADA" Cursor="Hand" AutoGenerateColumns="False"
RowDetailsVisibilityMode="Visible" >

我正在使用 http://www.amazedsaint.com/2010/10/asynchronous-delegate-command-for-your.html在我的 View 模型中实现异步方式。

这是我的 View 模型代码:

public class MainWindowViewModel:WorkspaceViewModel,INotifyCollectionChanged
{

MatchBLL matchBLL = new MatchBLL();
EfesBetServiceReference.EfesBetClient proxy = new EfesBetClient();

public ICommand DoSomethingCommand { get; set; }
public MainWindowViewModel()
{
DoSomethingCommand = new AsyncDelegateCommand(
() => Load(), null, null,
(ex) => Debug.WriteLine(ex.Message));
_matchObsCollection = new ObservableCollection<EfesBet.DataContract.GetMatchDetailsDC>();

}

List<EfesBet.DataContract.GetMatchDetailsDC> matchList;
ObservableCollection<EfesBet.DataContract.GetMatchDetailsDC> _matchObsCollection;

public ObservableCollection<EfesBet.DataContract.GetMatchDetailsDC> MatchObsCollection
{
get { return _matchObsCollection; }
set
{
_matchObsCollection = value;
OnPropertyChanged("MatchObsCollection");
}
}
//
public void Load()
{
matchList = new List<GetMatchDetailsDC>();
matchList = proxy.GetMatch().ToList();

foreach (EfesBet.DataContract.GetMatchDetailsDC match in matchList)
{
_matchObsCollection.Add(match);
}

}

正如您在我的 ViewModel 中的 Load() 方法中看到的那样,我首先从我的服务中获取 matchList(这是一个 DataContract 类的列表)。然后通过 foreach 循环,我将我的 matchList 项目插入到我的 _matchObsCollection(其中是 DataContract 类的 ObservableCollection)。现在我收到上述错误(如标题所示)“这种类型的 CollectionView 不支持从不同于 Dispatcher 线程的线程更改其 SourceCollection” enter image description here

任何人都可以建议我任何解决方案。此外,如果可能的话,我想知道如何在 View 中绑定(bind)我的 DataGrid 并在有更好的方法时异步刷新它。

60 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com