几天前我创建了这个线程,因为我无法从另一个线程更新 ObservableCollection。这是来自线程的解决方案:
Dispatcher.CurrentDispatcher.BeginInvoke(new Action(delegate
{
TheTVDB theTvdb = new TheTVDB();
foreach (TVSeries tvSeries in theTvdb.SearchSeries("Dexter"))
{
this.Overview.Add(tvSeries);
}
}),
DispatcherPriority.Background);
但是,这似乎不是真正的解决方案,因为 UI 在执行委托时仍然冻结。我的猜测是,上面并没有真正在另一个线程上运行任何东西,而是将它全部分派给 UI 线程。所以我真正想做的是自己创建一个新线程并进行加载(这发生在theTvdb.SearchSeries()
)。然后我将遍历结果并将其添加到我的ObservableCollection
,这必须发生在 UI 线程上。
这种方法听起来对吗?
我想出了下面我认为会加载结果并将它们添加到 ObervableCollection 并在我的列表视图中显示它们而不会冻结 UI 的方法。
Thread thread = new Thread(new ThreadStart(delegate
{
TheTVDB theTvdb = new TheTVDB();
List<TVSeries> dexter = theTvdb.SearchSeries("Dexter");
foreach (TVSeries tvSeries in dexter)
{
Dispatcher.CurrentDispatcher.BeginInvoke(new Action(delegate
{
this.Overview.Add(tvSeries);
}),
DispatcherPriority.Normal);
}
}));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
以上不会产生任何错误。相反,什么也没有发生。UI 不会冻结,但不会更新。Overview
UI 中没有显示其中的对象,我已经测试了绑定是否正确。如果我不加载它们并将它们添加到ObservableCollection
另一个线程上,这些对象将正确显示。
我尝试过的另一个解决方案是使用此答案中的 MTObservableCollection 来解决类似问题。当使用 的那个子类时ObservableCollection
,我自己没有发送任何东西。这给了我以下错误:
必须在与 DependencyObject 相同的线程上创建 DependencySource。
谁能告诉我我该怎么做:
- 在单独的线程上加载一些东西
- 使用步骤 1 的结果更新绑定到列表视图的 ObservableCollection
- 在 UI 中显示结果而不冻结 UI
我希望你能进一步帮助我。
更新:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:acb="clr-namespace:AttachedCommandBehavior"
mc:Ignorable="d"
x:Class="TVSeriesLibrary.OverviewView"
x:Name="UserControl"
d:DesignWidth="512"
d:DesignHeight="480">
<UserControl.Resources>
<DataTemplate x:Key="CoverTemplate">
<StackPanel Orientation="Horizontal">
<Image Width="82" Height="85" Stretch="Fill" Source="{Binding Cover}" Margin="10,10,0,10"/>
</StackPanel>
</DataTemplate>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="#515050">
<Grid.Resources>
<ResourceDictionary>
<Style x:Key="ItemContStyle" TargetType="{x:Type ListViewItem}">
<Setter Property="Background" Value="#282828" />
<Setter Property="Margin" Value="0,0,0,5" />
<Setter Property="Padding" Value="0" />
</Style>
</ResourceDictionary>
</Grid.Resources>
<ListView Height="112"
Width="488"
Margin="12,150,12,218"
Foreground="#ffffff"
Background="#515050"
VerticalContentAlignment="Center"
BorderThickness="0"
ItemTemplate="{StaticResource CoverTemplate}"
ItemsSource="{Binding Overview}">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
<ListView Height="170"
Margin="10,298,10,0"
VerticalAlignment="Center"
Foreground="#ffffff"
Background="#515050"
VerticalContentAlignment="Center"
BorderThickness="0"
Width="488" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ItemsSource="{Binding Path=Overview}"
SelectedItem="{Binding Path=SelectedTVSeries}"
ItemContainerStyle="{StaticResource ItemContStyle}">
<ListView.Resources>
<ResourceDictionary>
<Style x:Key="hiddenStyle" TargetType="GridViewColumnHeader">
<Setter Property="Visibility" Value="Collapsed"/>
</Style>
</ResourceDictionary>
</ListView.Resources>
<ListView.View>
<GridView>
<GridViewColumn Header="Cover" Width="auto" HeaderContainerStyle="{StaticResource hiddenStyle}">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding Path=Cover}" Height="50" Margin="-6,0,0,0" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Title" Width="200" HeaderContainerStyle="{StaticResource hiddenStyle}">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}" FontWeight="Bold"></TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Year" Width="100" HeaderContainerStyle="{StaticResource hiddenStyle}">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=DisplayYear}"></TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Button" Width="135" HeaderContainerStyle="{StaticResource hiddenStyle}">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Content="Details" Width="100" Height="20" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
</UserControl>