【发布时间】:2020-02-18 17:11:37
【问题描述】:
我遇到了一个错误(但我的代码仍在运行,所以直到现在我一直忽略它)说我的 BindableListView is not usable as an object element because it is not public or does not define a public parameterless constructor or a type converter.
我猜这是导致我再次崩溃的原因:System.ObjectDisposedException: Cannot access a disposed object. Object name: '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 设置为 Recycle Element 的正确方法吗?如果是这样,那么我将如何解决我的错误?
或者我应该从 BindableListView.cs 中的构造函数中删除参数以使其无参数,然后只添加一行 CachingStrategy = "RecycleElement" ?我试过这个,至少错误消失了..(虽然应用程序现在无法运行,“缺少默认 ctor”)但我不知道这是否是正确的做法。如果这是我应该做的,这也会解决我的第二个错误吗?
谢谢!
【问题讨论】:
标签: c# android xaml xamarin.forms