在异步方法中设置ItemsSource集合时,MVVM WPF接口冻结

时间:2018-04-11 10:40:10

标签: c# wpf listview mvvm async-await

我有一个ListView的问题。设置为ItemsSource的 <body> <button onclick="myFunction()">Yes</button> <div id="myDIV"> Can someone please help me? </div> <script> function myFunction() { var x = document.getElementById("myDIV"); if (x.style.display === "none") { x.style.display = "block"; } else { x.style.display = "none"; } } </script> </body> 时,应用程序会冻结几秒钟。

这是列表视图:

new Scaffold(
            appBar: new AppBar(
              automaticallyImplyLeading: false,
            )
)

这是填充条目列表的方法:

ObservableCollection

我确信没有CPU绑定操作,只有在分配了 <UserControl.Resources> <CollectionViewSource x:Key="Sets" Source="{Binding Path=Entries}"> <CollectionViewSource.GroupDescriptions> <PropertyGroupDescription PropertyName="DailySet.WeeklySet" /> <PropertyGroupDescription PropertyName="DailySet" /> </CollectionViewSource.GroupDescriptions> </CollectionViewSource> <utilities:InvertableBooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/> </UserControl.Resources> <ListView ItemsSource="{Binding IsAsync=True, UpdateSourceTrigger=PropertyChanged}" DataContext="{StaticResource Sets}" > <ListView.View > <GridView > <GridViewColumn DisplayMemberBinding="{Binding StartTimeFormatted}" Header="Start" Width="60" /> <GridViewColumn DisplayMemberBinding="{Binding EndTimeFormatted}" Header="End" Width="60" /> <GridViewColumn DisplayMemberBinding="{Binding TotalHoursFormatted}" Header="Time" Width="80" /> <GridViewColumn DisplayMemberBinding="{Binding CostCode}" Header="CostCode" Width="120" /> <GridViewColumn DisplayMemberBinding="{Binding Operation}" Header="Operation" Width="160" /> <GridViewColumn DisplayMemberBinding="{Binding Comment}" Header="Comment" > <GridViewColumn.Width> <MultiBinding Converter="{StaticResource LastColumnMaximizerConverter}"> <Binding Path="ActualWidth" RelativeSource="{RelativeSource AncestorType=ListView}"/> <Binding Path="View.Columns" RelativeSource="{RelativeSource AncestorType=ListView}"/> </MultiBinding> </GridViewColumn.Width> </GridViewColumn> <GridViewColumn Header="Action" Width="60"> <GridViewColumn.CellTemplate> <DataTemplate > <StackPanel Orientation="Horizontal" > <Button IsEnabled="{Binding ElementName=thisUc, Path=DataContext.EditingAllowed}" BorderThickness="0" Margin="2" VerticalAlignment="Center" HorizontalAlignment="Center" Background="Transparent" ToolTip="Edit this entry" Command="{Binding ElementName=thisUc, Path=DataContext.EditEntryCommand}" CommandParameter="{Binding}"> <Button.Content> <Image Height="18" Width="20" Source="pack://application:,,,/Gfx/edit.png"></Image> </Button.Content> </Button> <Button IsEnabled="{Binding ElementName=thisUc, Path=DataContext.EditingAllowed}" BorderThickness="0" Margin="2" VerticalAlignment="Center" HorizontalAlignment="Center" Background="Transparent" ToolTip="Delete this entry" Command="{Binding ElementName=thisUc, Path=DataContext.DeleteEntryCommand}" CommandParameter="{Binding}"> <Button.Content> <Image Height="18" Width="20" Source="pack://application:,,,/Gfx/delete.png"></Image> </Button.Content> </Button> </StackPanel> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> </GridView> </ListView.View> <ListView.GroupStyle> <GroupStyle> <GroupStyle.ContainerStyle> <Style TargetType="{x:Type GroupItem}"> <Setter Property="Margin" Value="0,0,0,5"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type GroupItem}"> <Expander IsExpanded="{Binding Path=Name.IsWeekOpen, Mode=OneWay}" > <Expander.Header> ... header label definition </Expander.Header> <Expander.Content> <ItemsPresenter /> </Expander.Content> </Expander> </ControlTemplate> </Setter.Value> </Setter> </Style> </GroupStyle.ContainerStyle> </GroupStyle> <GroupStyle> <GroupStyle.ContainerStyle> <Style TargetType="{x:Type GroupItem}"> <Setter Property="Margin" Value="10,0,10,5"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type GroupItem}"> <Expander IsExpanded="False" BorderBrush="#FFA4B97F" BorderThickness="0,0,0,1" IsEnabled="{Binding Path=Name.DayDetailsGroupingHeader.RealEntriesAvailable}"> <Expander.Header> ... header label definition </Expander.Header> <Expander.Content> <ItemsPresenter /> </Expander.Content> </Expander> </ControlTemplate> </Setter.Value> </Setter> </Style> </GroupStyle.ContainerStyle> </GroupStyle> </ListView.GroupStyle> </ListView> 集合时才会出现问题。 我尝试用空集合替换public override async Task LoadLiveData() { IEnumerable<DailyDetailedInfo> details = await this.Connector.GetDetails(); List<DailyDetailEntry> entries = this.Mapper.Map<IEnumerable<DailyDetailedInfo>, IEnumerable<DailyDetailEntry>>(details).ToList(); //up to this point the execution takes ~300-500 miliseconds this.Entries = new ObservableCollection<DailyDetailEntry>(entries.OrderBy(x => x.ExtractedStartDate)); // collection assigning takes few miliseconds as well //this.Entries = new ObservableCollection<DailyDetailEntry>(); when empty collection is assigned, the UI is not frozen, even though the Connector.GetDetails() is called } 赋值,在这种情况下它可以正常工作。

我也尝试逐个添加项目,但没有变化:

this.Entries

我应该怎么做才能让用户界面响应?

0 个答案:

没有答案