简体   繁体   English

使用异步/等待,但UI仍然使用Caliburn.Micro BindableCollection AddRange挂起

[英]Using async/await but UI still hangs using Caliburn.Micro BindableCollection AddRange

I have facing a problem where my UI is unresponsive for a one sec or so and I guess its my Async/Await skills that are not upgraded to latest version. 我遇到了一个问题,即我的UI在一秒左右时间内没有响应,我猜它的Async / Await技能还没有升级到最新版本。

Its a WPF app using the Caliburn Micro Framework. 它是使用Caliburn Micro Framework的WPF应用程序。 What I have found sofar is that my UI is responsive while it is seaching packages (Nuget.Core) and its when the Task have returned with a list of elements and its doign AddRange on the bindable collection. 我发现sofer的是,我的UI在搜索程序包(Nuget.Core)时是响应的,并且当任务返回时带有绑定的元素列表及其在可绑定集合中占主导的AddRange。 (Its on a feed with max 5 elements, so I dont see why it should take more then a few ms to put that few elements into the collection. (它的Feed中最多包含5个元素,因此我不明白为什么要将这几个元素放入集合中要花更多的时间才能完成。

    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));
    }

Here is the 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>

Not a perfect solution but atleast my UI is responsive again. 这不是一个完美的解决方案,但我的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);
        }  
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Caliburn.Micro BindableCollection 未在 View 代码中更新,使用后面的代码设置绑定 - Caliburn.Micro BindableCollection is not updating in View code using code behind to set up bindings C# WPF 使用 Caliburn.Micro:如何将 IEnumerable 转换为 BindableCollection? - C# WPF using Caliburn.Micro: How can I convert an IEnumerable to BindableCollection? Caliburn.Micro问题:PropertyChangedBase,BindableCollection和AutoMapper - Caliburn.Micro issue: PropertyChangedBase, BindableCollection and AutoMapper 将ProgressBar与Caliburn.micro一起使用 - Using ProgressBar with Caliburn.micro 使用UWP中的Caliburn.Micro从构造函数进行异步方法调用 - Async method call from constructor using Caliburn.Micro in UWP 使用caliburn micro和BindableCollection从代码在组合框中设置selecteditem - Set selecteditem in combobox from code using caliburn micro with BindableCollection 是否可以序列化caliburn.micro bindablecollection版本3.0.3? - Is it possible to serialize caliburn.micro bindablecollection ver 3.0.3? 使用Caliburn.Micro的视图模型中的气泡事件 - Bubble events in viewmodels using Caliburn.Micro 将PCL MEF2与Caliburn.Micro一起使用 - Using PCL MEF2 with Caliburn.Micro 使用caliburn.micro显示2个ViewModel - Displaying 2 ViewModels using caliburn.micro
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM