微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ListView CachingStrategy RecycleElement 问题 - GC 每秒调用一次,应用程序变慢

如何解决ListView CachingStrategy RecycleElement 问题 - GC 每秒调用一次,应用程序变慢

我在 Xamarin Forms 中遇到了 ListView 的缓存策略问题。我将缓存策略设置为 recycleelement,我的视图单元使用带有 lottie 文件自定义控件,代码如下:

<ViewCell>
        <controls:CheckBoxLottie IsChecked="{Binding checkBoxChecked}" />
</ViewCell>

我的自定义控件的代码如下所示:

  public class CheckBoxLottie : AnimationView
    {
        public static BindableProperty IsCheckedProperty = BindableProperty.Create(nameof(IsChecked),typeof(bool),typeof(CheckBoxLottie),defaultBindingMode: BindingMode.TwoWay,propertyChanged: IsCheckedChanged);

        public bool IsChecked
        {
            get { return (bool)GetValue(IsCheckedProperty); }
            set { SetValue(IsCheckedProperty,value); }
        }
        static void IsCheckedChanged(BindableObject bindable,object oldValue,object newValue)
        {
            if (!(bindable is CheckBoxLottie cb))
                return;

            if ((bool)newValue)
            {
                cb.PlayMinAndMaxProgress(0.98f,0.99f);
            }
            else
            {
                cb.PlayMinAndMaxProgress(0.0f,0.015f);
            }
        }

        public CheckBoxLottie()
        {
            Animation = "checkBox.json";
            Autoplay = false;
            Clicked += CheckBox_OnClick;
        }

        void CheckBox_OnClick(object sender,EventArgs e)
        {
            IsChecked = !IsChecked;
        }
    }

问题是当我将ListView的cachingstrategy设置为recycleelement时,我的构造函数一直被调用,即使我删除了带有listview的页面。它会导致内存泄漏并降低我的应用性能

我是否遗漏了自定义控件代码中的某些内容?当我使用列表视图删除页面时,如何从内存中释放它?我认为垃圾收集器会为我做这件事。

这是我的 xaml 页面代码

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:models="clr-namespace:MyApp.Model"
             xmlns:controls="clr-namespace:MyApp.Controls" 
             mc:Ignorable="d"
             x:Class="MyApp.Views.ListPage">
    <ContentPage.Resources>
        <DataTemplate x:Key="toolTipTemplate2"  x:DataType="models:MyItem">
            <ViewCell>
                    <controls:CheckBoxLottie IsChecked="{Binding checkBoxChecked}"/>
            </ViewCell>
        </DataTemplate>
    </ContentPage.Resources>
    
    <ContentPage.Content>
        
            <ListView SeparatorColor="Transparent" 
                          CachingStrategy="RecycleElement"
                          RowHeight="{OnIdiom 190,Phone=190,Tablet=380}"
                              x:Name="listView"
                         VerticalScrollBarVisibility="Always"
                          
                  BackgroundColor="Transparent"
                          SelectionMode="None" ItemTemplate="{StaticResource toolTipTemplate2}" />
    </ContentPage.Content>
</ContentPage>

这里是 .cs 文件

  [XamlCompilation(XamlCompilationoptions.Compile)]
    public partial class ListPage: ContentPage
    {
        public ListPage(List<MyItem> list)
        {
            InitializeComponent();
            listView.ItemsSource = list;
        }

    }

我还注意到,在输出中这些行一直被打印:

[Mono] GC_TAR_BRIDGE bridges 36 objects 36 opaque 0 colors 36 colors-bridged 36 colors-visible 36 xref 0 cache-hit 0 cache-semihit 0 cache-miss 0 setup 0.19ms tarjan 0.04ms scc-setup 0.05ms gather-xref 0.02ms xref-setup 0.01ms cleanup 0.15ms
[Mono] GC_BRIDGE: Complete,was running for 26.75ms
[Mono] GC_MInor: (Nursery full) time 5.79ms,stw 8.16ms promoted 786K major size: 11600K in use: 10156K los size: 10240K in use: 8644K

当缓存策略为 recycleelement 时,垃圾收集器似乎无法清理内存。

这里是带标签的viewcell,它也一直在调用GC。

<ViewCell>
                    <Label Text="{Binding name}"   
                                TextColor="{Binding TextColor}" 
                                 FontSize="{OnIdiom 24,Phone=24,Tablet=48}"/>
</ViewCell>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。