C# 设置CachingStrategy以Xamarin形式回收元素的正确方法是什么?

C# 设置CachingStrategy以Xamarin形式回收元素的正确方法是什么?,c#,android,xaml,xamarin.forms,C#,Android,Xaml,Xamarin.forms,我一直收到一个错误,但我的代码仍然与它一起运行,所以我一直忽略它,直到现在,我说我的BindableListView不能用作对象元素,因为它不是公共的,或者没有定义公共的无参数构造函数或类型转换器 我猜这是另一个崩溃的原因:System.ObjectDisposedException:无法访问已处置对象。对象名称:“Xamarin.Forms.Platform.Android.FormsTextView” 因为我在网上看到一些建议,大多数时候,这个问题与ListView有关,我应该将Cachin

我一直收到一个错误,但我的代码仍然与它一起运行,所以我一直忽略它,直到现在,我说我的BindableListView不能用作对象元素,因为它不是公共的,或者没有定义公共的无参数构造函数或类型转换器

我猜这是另一个崩溃的原因:System.ObjectDisposedException:无法访问已处置对象。对象名称:“Xamarin.Forms.Platform.Android.FormsTextView” 因为我在网上看到一些建议,大多数时候,这个问题与ListView有关,我应该将CachingStrategy设置为RecycleElement

所以我决定最后看一看BindableListView并添加CachingStrategy=RecycleElement

现在已经实现了一些代码,它们似乎已经在尝试添加CachingStrategy:

BindableListView.cs:

namespace MyApp.Core.Controls
{
    [PropertyChanged.DoNotNotify]
    public class BindableListView : ListView
    {
        public static BindableProperty ItemClickedProperty = BindableProperty.Create(nameof(ItemClicked), typeof(ICommand), typeof(BindableListView));
        public static BindableProperty ItemAppearsProperty = BindableProperty.Create(nameof(ItemAppears), typeof(ICommand), typeof(BindableListView));
        public static BindableProperty AnimateScrollToSelectedItemProperty = BindableProperty.Create(nameof(AnimateScrollToSelectedItem), typeof(bool), typeof(BindableListView), true);

        /// <summary>
        /// Constructs a <see cref="BindableListView"/>
        /// </summary>
        /// <param name="cachingStrategy">Sets the caching strategy for the <see cref="ListView"/>.</param>
        /// <example><![CDATA[
        /// <controls:BindableListView>
        ///  <x:Arguments>
        ///    <ListViewCachingStrategy>RecycleElement</ListViewCachingStrategy>
        ///  </x:Arguments>
        /// </controls:BindableListView>
        /// ]]></example>
        public BindableListView(ListViewCachingStrategy cachingStrategy)
            : base(cachingStrategy)
        {
            ItemTapped += OnItemTapped;
            ItemAppearing += OnItemAppearing;
            ItemSelected += OnItemSelected;
    ...
然后在使用此ListView的所有.xaml文件中:

<controls:BindableListView
      ...>
      <x:Arguments>
        <ListViewCachingStrategy>RecycleElement</ListViewCachingStrategy>
      </x:Arguments>
这是将CachingStrategy设置为回收元素的正确方法吗?如果是这样的话,我将如何着手修复我的错误

或者我应该从BindableListView.cs中的构造函数中删除该参数以使其无参数,然后只添加一行CachingStrategy=RecycleElement吗?我试过了,错误至少消失了。。虽然应用程序现在不会运行,缺少默认的ctor,但我不知道这样做是否正确。如果这是我应该做的,这会修复我的第二个错误吗


谢谢

根据您的代码,我认为将CachingStrategy设置为Recycle元素是正确的方法

根据这份文件:

此问题的解决方案是在子类ListView上指定一个构造函数,该构造函数接受ListViewCachingStrategy参数并将其传递到基类:

public class CustomListView : ListView
{

   public CustomListView (ListViewCachingStrategy strategy) : base (strategy)
    {

    }
    ...
}
然后,可以使用x:Arguments语法从XAML中指定ListViewCachingStrategy枚举值

<local:CustomListView>
<x:Arguments>
    <ListViewCachingStrategy>RecycleElement</ListViewCachingStrategy>
</x:Arguments>

适用于您的情况的错误消息部分是:

。。。未定义公共无参数构造函数

更改此项:

public BindableListView(ListViewCachingStrategy cachingStrategy)
            : base(cachingStrategy)
{
    ItemTapped += OnItemTapped;
    ItemAppearing += OnItemAppearing;
    ItemSelected += OnItemSelected;
    ...
}
为此:

public BindableListView(ListViewCachingStrategy cachingStrategy)
        : base(cachingStrategy)
{
    Init();
}

public BindableListView()
        : base()
{
    Init();
}

private void Init()
{
    ItemTapped += OnItemTapped;
    ItemAppearing += OnItemAppearing;
    ItemSelected += OnItemSelected;
    ...
}
请注意,您确实希望保留参数化构造函数XAML所需要的;因此,解决方案是添加第二个无参数构造函数

因此,您将拥有所需的两种形式的构造函数,并且它们中的每一种都将执行您的初始化逻辑