Xamarin.Forms - Force ListView layout to redraw

若如初见. 提交于 2019-12-04 03:24:23

问题


I have a ListView with HasUnevenRows = true, where the content of each cell is variant to begin with, but the content can also change on the fly (through clicking of a button in the cell). When the ListView initially loads, the system must perform a calculation in order to correctly determine and draw the heights of each cell based on their content.

But when I update the content after the initial load, this same calculation is apparently not done, because the height of the cell does not change -- until I've scrolled that item out of view and then back into view.

How can I force this same calculation and redraw manually, without completely refreshing the ListView??

I tried wrapping the ListView in a Frame and calling .ForceLayout() on the frame, but no luck.


回答1:


The height of the cells aren't recalculated by default because it is an expensive process. If you want to force a re-size you need to call ForceUpdateSize() on the cell.

NOTE: This does not work on older versions of Xamarin Forms. It was added in 2.0.0.0, but try updating to at least 2.2.0.31




回答2:


I've had terrible luck getting native controls to redraw. The one thing that has worked for me has been in a custom renderer: ((IVisualElementController)Element).NativeSizeChanged()




回答3:


Set your CachingStrategy="RetainElement", it's expensive, but will force an update a lot more. If your list isn't too long, you won't notice.

I've noticed that if a cell is visible, even though bound to an obserablecollection, that cell doesn't update until I scroll up then down.




回答4:


You have to use DataBinding if you want your view to refresh automatically..




回答5:


        YourListView.BeginRefresh ();
        YourListView.EndRefresh ();

But i strongly recommend not to do this.



来源:https://stackoverflow.com/questions/37414063/xamarin-forms-force-listview-layout-to-redraw

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!