简体   繁体   English

将新项目添加到 ObservableCollection 后,ListView 不会立即更新

[英]ListView does not update immediately after I add a new item to the ObservableCollection

I am learning Xamarin.Forms and I have created a new project with Visual Studio 2019, using the Master-Detail project template.我正在学习 Xamarin.Forms,我使用 Master-Detail 项目模板使用 Visual Studio 2019 创建了一个新项目。 I am using UWP (as I want to make a Windows 10 app).我正在使用 UWP(因为我想制作一个 Windows 10 应用程序)。

Why does the list view not show up immediately after I add the new item?为什么添加新项目后列表视图没有立即显示? Is this caused by slowness in the event/message passing, or is there an animation that is slowing it down on purpose?这是由于事件/消息传递缓慢引起的,还是有故意减慢速度的动画?

In this sample, everything is happening in a local ObservableCollection , and there are only a few items in it, so I expect it to be able to display the change immediately.在此示例中,一切都发生在本地ObservableCollection中,并且其中只有几项,因此我希望它能够立即显示更改。

Example:例子:

显示将项目添加到绑定到 ListView 的 ObservableCollection

The code is generated by Visual Studio, I can provide it, but I haven't really made any change.代码由 Visual Studio 生成,我可以提供,但我并没有真正进行任何更改。

EDIT: Since I suspect this is related to the events generated by the ObservableCollection and the bindings in the ListView, I tried the following change, which worked:编辑:因为我怀疑这与 ObservableCollection 生成的事件和 ListView 中的绑定有关,所以我尝试了以下更改,该更改有效:

MessagingCenter.Subscribe<NewItemPage, Item>(this, "AddItem", async (obj, item) =>
{
    var newItem = item as Item;
    Items.Add(newItem);
    await DataStore.AddItemAsync(newItem);
});

To:到:

MessagingCenter.Subscribe<NewItemPage, Item>(this, "AddItem", async (obj, item) =>
{
    var newItem = item as Item;
    var tmp = new ObservableCollection<Item>();
    foreach (var i in Items)
    {
        tmp.Add(i);
    }
    tmp.Add(newItem);
    Items = tmp;
    OnPropertyChanged(nameof(Items));
    await DataStore.AddItemAsync(newItem);
});

Of course, this is inefficient and not ideal but might help someone else figure out what is going on in the original version.当然,这是低效且不理想的,但可能会帮助其他人弄清楚原始版本中发生了什么。 Behaviour after the change:更改后的行为:

更改后立即加载 ListView

In UWP the views for the navigation back stack are not kept in memory to conserve it, so when you navigate to another page and back, the previous page is reconstructed.在 UWP 中,导航返回堆栈的视图不会保存在内存中以保存它,因此当您导航到另一个页面并返回时,将重建前一个页面。 In this case however, the reason why it "fades" in is animation, that is built into the default NavigationPageRenderer in UWP.然而,在这种情况下,它“淡入”的原因是动画,它内置于 UWP 中的默认NavigationPageRenderer中。

The animation is occurring here in the code , it should be possible to disable the animation by setting the isAnimated flag to false on the right place, but I don't have my dev device with me right now so I can't test it.动画发生在代码中,应该可以通过在正确的位置将isAnimated标志设置为false来禁用动画,但我现在没有我的开发设备,所以我无法测试它。

Update更新

It seems that this is really a problem with the UWP implementation of either ListView or MasterDetailPage .看来这确实是ListViewMasterDetailPage的 UWP 实现的问题。

Workaround解决方法

Adding a small delay to the message handler subscription fixes the issue:向消息处理程序订阅添加一个小的延迟可以解决这个问题:

MessagingCenter.Subscribe<NewItemPage, Item>(this, "AddItem", async (obj, item) =>
{
    await Task.Delay(100);
    var newItem = item as Item;
    Items.Add(newItem);
    await DataStore.AddItemAsync(newItem);
});

I have reported the problem on GitHub as an issue .我已将 GitHub 上的问题报告为问题

If you are really using Xamarin.Forms , with the Master-Detail template, and if you " would like [the page] to show up as quick as possible, without going through a "white fade " phase", you should go to the Views.NewItemPage.xaml.cs file and change the Save_Clicked event handler as follows: 如果您确实使用带有Master-Detail模板的Xamarin.Forms ,并且“ 如果希望 [页面] 尽可能快地显示,而无需经历“白色渐变 ”阶段,则应转到Views.NewItemPage.xaml.cs文件, Save_Clicked如下所示更改Save_Clicked事件处理程序:

await Navigation.PopModalAsync(animated: false);

By doing this, the "fade" animation is not presented, and instead you are pop back to the previous page immediately. 这样,将不显示“淡入淡出”动画,而是立即弹出您回到前一页。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM