简体   繁体   English

Xamarin表单-重绘视图

[英]Xamarin Forms - Redraw view

I am writing an app using Xamarin Forms XAML, and I need a way to redraw the view. 我正在使用Xamarin Forms XAML编写应用程序,并且需要一种重绘视图的方法。

I have a layout containing multiple Labels and a ListView. 我有一个包含多个Label和一个ListView的布局。 I also have a button that when pressed calls an API to download a new set of data which needs to update the controls on the page. 我还有一个按钮,当按下该按钮时会调用API以下载一组新的数据,这些数据需要更新页面上的控件。

All of my controls are bound correctly so when the data updates in the background, the data does update on the view but the controls are not redrawn and therefore messes up the look of the layout. 我所有的控件都正确绑定,因此,当数据在后台更新时,数据确实会在视图上更新,但是控件不会重绘,因此会弄乱布局的外观。

Sometimes the data in my ListView can be only 1 or 2 rows but usually will be a lot more. 有时,我的ListView中的数据只能是1或2行,但通常会更多。 The 1st time the page is drawn, everything looks fine but my problem is when updating the view with data that has a different number of rows in the ListView as the controls do not resize. 第一次绘制页面时,一切看起来都很好,但是我的问题是,使用控件中不调整大小的ListView中具有不同行数的数据更新视图时。

Example: 例:

When 1st entering the screen I download data and the ListView has 10 items and so draws 10 rows on the screen 第一次进入屏幕时,我下载了数据,并且ListView有10个项目,因此在屏幕上绘制了10行

When I press the "Reload Data" button, the data downloaded might now only contain 2 items. 当我按下“重新加载数据”按钮时,下载的数据现在可能仅包含2个项目。 The data is updated correctly but the spacing on the screen is still the same size as when 1st drawn (with the 10 items), so I now have a large blank space. 数据已正确更新,但屏幕上的间距仍与第一个绘制时(带有10个项目)的尺寸相同,所以我现在有很大的空白空间。

If the opposite of this example is done (1st entry is 2 items, 2nd entry is 10 items), then the space for the ListView is still only 2 items high and therefore you can only see 2 of the 10 items. 如果完成本示例的相反操作(第一个条目为2个项目,第二个条目为10个项目),则ListView的空间仍然只有2个项目高,因此您只能看到10个项目中的2个。

What I want to be able to do, is to refresh the controls somehow when downloading new data so that the size of the ListView is redrawn and is always the correct size for the amount of items being displayed. 我要能够做的是,在下载新数据时以某种方式刷新控件,以便重新绘制ListView的大小,并且始终是所显示项目数量的正确大小。

Had the same issue. 有同样的问题。 I think you are approaching it wrong. 我认为您的做法不对。 ListView doesn't work the way you think it does. ListView无法按您认为的方式工作。 It's not designed to always fit just perfectly for it's content. 它并非旨在始终完全适合其内容。 If you want that, you need to calculate the height by yourself and set it to the ListView . 如果需要,您需要自己计算高度并将其设置为ListView

One approach is to change the height of your ListView . 一种方法是更改ListView的高度。 You can achieve that by binding a property to HeightRequest : 您可以通过将属性绑定到HeightRequestHeightRequest

<ListView
  HeightRequest="{Binding ListViewHeight}">
</ListView>

This is my calculation for ListViewHeight . 这是我对ListViewHeight计算。 RowHeight is 72 for Android and 75 for iOS. 对于Android, RowHeight是72;对于iOS, RowHeight是75。 It's a dirty solution because of hard coded values for height, but it's the best solution I found for the problem. 由于高度的硬编码值,这是一个肮脏的解决方案,但这是我找到的最佳解决方案。 Keep in mind that you need to adjust these values to fit your needs, otherwise your ListView might be too small/tall. 请记住,您需要调整这些值以满足您的需求,否则ListView可能太小/太高。

public double ListViewHeight
{
    get
    {
        if (MyItemsList!= null)
        {
            double listViewHeight = MyItemsList.Count * RowHeight;
            if (CanLoadMoreNotifications)
            {
                listViewHeight += RowHeight;
            }
            return listViewHeight;
        }
        return 0;
    }
}

Now every time you're making changes to you list, call RaisePropertyChanged("ListViewHeight"); 现在,每次对列表进行更改时,请调用RaisePropertyChanged("ListViewHeight"); . This way, your ListView height is calculated and your UI updates. 这样,将计算出ListView高度并更新UI。

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

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