【问题标题】:Object retain in memory when click on listview headertemplate in iOS device在 iOS 设备中单击 listview headertemplate 时对象保留在内存中
【发布时间】:2020-10-28 13:06:18
【问题描述】:

我正在使用 Xamarin.Forms 开发企业应用程序。我在 iOS 中特别面临内存泄漏问题(在 Android 中运行良好)。

关于单击列表视图项目标题,我实现了手势,然后还实现了一个透明按钮,但它会导致内存泄漏 - 即使我注释掉它的功能。

如果我评论 testbutton 那么它的对象可以正常处理。我可以做些什么来解决这个问题?

 ```
    <ListView
                    Style="{DynamicResource BaseListViewStyle}"
                    IsGroupingEnabled="True"
                    ItemsSource="{Binding Deliveries.ExpandedDeliveryItemModels}"
                    IsPullToRefreshEnabled="True"
                    IsRefreshing="{Binding Deliveries.IsRefreshing}"
                    RefreshCommand="{Binding RefreshCommand}"
                    SelectionMode="None">
                    <ListView.GroupHeaderTemplate>
                        <DataTemplate>
                            <ViewCell AutomationId="headerCell">
                                <StackLayout AutomationId="headerLayout" Orientation="Horizontal">
                                    <Label  Text="{Binding HeaderTitle}"
                                            AutomationId="DeliveriesToday"
                                            HorizontalOptions="StartAndExpand">
                                    </Label>
                                    <Button x:Name="testbutton"
                                            HorizontalOptions="End"
                                            Command="{Binding Source={x:Reference deliveriesPage}, Path=BindingContext.HeaderClickCommand}"
                                            CommandParameter="{Binding .}">
                                    </Button>
                                </StackLayout>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.GroupHeaderTemplate>
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell AutomationId="deliveriesCell">
                                <Label Text="Testing"></Label>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
    ```

【问题讨论】:

    标签: xamarin xamarin.forms xamarin.ios xamarin-studio xamarin.forms.listview


    【解决方案1】:

    欢迎来到 SO!

    你可以试试修改ListViewCachingStrategyCachingStrategy值有3种。

    • RecycleElement 1 表示不需要的单元格将其绑定上下文更新为需要的单元格的绑定上下文。

    • RecycleElementAndDataTemplate 3 表示除了由 RecycleElement 指定的行为外,由 DataTemplateSelector 选择的 DataTemplate 对象按数据模板类型进行缓存。

    • RetainElement 0 表示对于列表视图的 ItemsSource 属性中的每个项目,将从 DataTemplate 构造一个唯一的元素。

    默认值为RetainElement,行为是在不需要时将项目数据保留在其生成的单元格中。

    出于性能原因,默认行为可能会在未来版本中更改为 RecycleElement。同时,出于内存和性能方面的考虑,应用开发者在构建新的 List View 时应指定 RecycleElement

    因此,您可以修改代码如下:

    <ListView CachingStrategy="RecycleElement" >
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <!-- ... -->
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    

    【讨论】:

    • @Rvkamboj Okey,你能在这里分享一个示例项目链接吗?我会检查的。
    • @Rvkamboj 另外,你能否分享一个.gif 来解释这个如果我评论 testbutton 那么它的对象处理得很好。 ?因为对此没有太多了解。这将有助于在我的 loacl 站点中重现此内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-11
    • 1970-01-01
    • 1970-01-01
    • 2014-11-10
    • 2014-08-28
    • 1970-01-01
    相关资源
    最近更新 更多