使用async / await但UI仍然使用Caliburn.Micro BindableCollection AddRange挂起

时间:2013-12-17 06:14:56

标签: c# wpf asynchronous async-await caliburn.micro

我遇到了一个问题,即我的用户界面在一秒左右没有响应,我想我的Async / Await技能未升级到最新版本。

它是使用Caliburn Micro Framework的WPF应用程序。我发现,我发现我的UI在检索包(Nuget.Core)时是响应的,当它返回时,任务返回了可绑定集合上的元素列表及其doign AddRange。 (它在一个包含最多5个元素的Feed中,所以我不明白为什么它需要花费几毫秒来将这些元素放入集合中。

    protected override async void OnActivate()
    {
        base.OnActivate();
        this.PropertyChanged += PluginManagerViewModel_PropertyChanged;            

        IsBusy = true;
        var list = await SearchPackages();
        this.Packages.AddRange(list);
        IsBusy = false;

    }
    async void  PluginManagerViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        switch (e.PropertyName)
        {
            case "SelectedVersion":
            case "SelectedSortBy":
               var list= await SearchPackages();
               this.Packages.AddRange(list);
                break;
            default:
                break;
        }

    }
    public BindableCollection<IPackage> Packages { get; set; }
    private Task<IEnumerable<IPackage>> SearchPackages()
    {
        return Task.Run(() =>
                SelectedSortBy.OrderBy(
                (_manager.SourceRepository
                .Search("", SelectedVersion != "Stable")))
                .AsEnumerable()
                .Where(PackageExtensions.IsListed));
    }

这是listview:

<ListView ScrollViewer.PanningMode="VerticalFirst"
                   ScrollViewer.VerticalScrollBarVisibility="Visible"
                   ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                   ScrollViewer.PanningRatio="0.5"
                   x:Name="Packages">
                   <ListView.ItemTemplate>
                    <DataTemplate>
                            <Grid cal:Bind.Model="{Binding}">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition/>
                                <ColumnDefinition Width="4*"/>
                            </Grid.ColumnDefinitions>
                            <TextBlock x:Name="Description" Grid.Column="1" Margin="10,27.96,136,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Height="62.04"/>
                            <TextBlock x:Name="Title" TextWrapping="Wrap"  VerticalAlignment="Top"  FontWeight="Bold" Grid.Column="1" Margin="10,10,136,0"/>
                            <Button Content="Install" HorizontalAlignment="Right" VerticalAlignment="Top" Width="121" Grid.Column="1" Margin="0,10,10,0"/>
                            <TextBlock Grid.Column="1" x:Name="Version" HorizontalAlignment="Right"  Margin="0,32.96,10,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" TextAlignment="Right" FontWeight="Bold"/>
                        </Grid>
                    </DataTemplate>
                   </ListView.ItemTemplate>
            </ListView>

1 个答案:

答案 0 :(得分:0)

不是一个完美的解决方案,但至少我的UI再次响应。

    private async Task UpdatePackagesCollection()
    {

        IsBusy = true;
        var list = await SearchPackages().ConfigureAwait(false);    
        this.Packages.Clear();
        IsBusy = false;
        foreach (var p in list)
        {
            await App.Current.Dispatcher.InvokeAsync(() =>
            {
                Packages.Add(p);

            }, DispatcherPriority.ContextIdle);
        }  
    }