Xamarin.Form FlowListView-刷新列表时发生NullReferenceException

时间:2018-11-22 00:27:27

标签: c# xamarin.forms

复制步骤:

从数据库或其他来源加载集合 从数据库中删除项目 例如,使用“拉动刷新”刷新列表 结果: 应用程序崩溃。

观察

如果新的刷新返回相同数量的项目,则该应用不会崩溃; 如果新的刷新返回零项,则该应用不会崩溃; 如果我离开页面并再次导航到该页面,则不会发生错误

版本:

DLToolkit.Forms.Controls.FlowListView:2.0.11

Xamarin.Forms:3.4.0.1008975

错误:

  

11-20 23:38:07.111 I / MonoDroid(10001):System.NullReferenceException:   你调用的对象是空的。 11-20   23:38:07.111 I / MonoDroid(10001):在   Xamarin.Forms.Internals.DataTemplateExtensions.CreateContent   (Xamarin.Forms.DataTemplate自我,System.Object项,   Xamarin.Forms.BindableObject容器)中的[0x00000]   D:\ a \ 1 \ s \ Xamarin.Forms.Core \ DataTemplateExtensions.cs:19 11-20   23:38:07.111 I / MonoDroid(10001):在   Xamarin.Forms.Internals.TemplatedItemsList2 [TView,TItem] .ActivateContent   (System.Int32索引,System.Object项)在[0x00000]中   D:\ a \ 1 \ s \ Xamarin.Forms.Core \ TemplatedItemsList.cs:534 11-20   23:38:07.111 I / MonoDroid(10001):在   Xamarin.Forms.Internals.TemplatedItemsList2 [TView,TItem] .CreateContent   (System.Int32索引,System.Object项,System.Boolean插入)   D:\ a \ 1 \ s \ Xamarin.Forms.Core \ TemplatedItemsList.cs:543中的[0x00000]   11-20 23:38:07.111 I / MonoDroid(10001):在   Xamarin.Forms.Internals.TemplatedItemsList2 [TView,TItem] .GetOrCreateContent   (System.Int32索引,System.Object项)[0x00023]在   D:\ a \ 1 \ s \ Xamarin.Forms.Core \ TemplatedItemsList.cs:602 11-20   23:38:07.111 I / MonoDroid(10001):在   Xamarin.Forms.Internals.TemplatedItemsList2 [TView,TItem] .get_Item   (System.Int32索引)[0x00000]在   D:\ a \ 1 \ s \ Xamarin.Forms.Core \ TemplatedItemsList.cs:337 11-20   23:38:07.111 I / MonoDroid(10001):在   Xamarin.Forms.Platform.Android.ListViewAdapter.GetCellsFromPosition   (System.Int32位置,System.Int32接受)[0x0003b]在   D:\ a \ 1 \ s \ Xamarin.Forms.Platform.Android \ Renderers \ ListViewAdapter.cs:538   11-20 23:38:07.111 I / MonoDroid(10001):在   Xamarin.Forms.Platform.Android.ListViewAdapter.GetCellForPosition   (System.Int32位置)在[0x00000]中   D:\ a \ 1 \ s \ Xamarin.Forms.Platform.Android \ Renderers \ ListViewAdapter.cs:453   11-20 23:38:07.111 I / MonoDroid(10001):在   Xamarin.Forms.Platform.Android.ListViewAdapter.GetView(System.Int32   位置,Android.Views.View convertView,Android.Views.ViewGroup   父级)[0x0006d]在   D:\ a \ 1 \ s \ Xamarin.Forms.Platform.Android \ Renderers \ ListViewAdapter.cs:225   11-20 23:38:07.111 I / MonoDroid(10001):在   Android.Widget.BaseAdapter.n_GetView_ILandroid_view_View_Landroid_view_ViewGroup_   (System.IntPtr jnienv,System.IntPtr native__this,System.Int32   位置System.IntPtr native_convertView System.IntPtr   native_parent)[0x00018]在<263adecfa58f4c449f1ff56156d886fd>:0 11-20中   23:38:07.111 I / MonoDroid(10001):at(包装动态方法)   System.Object.287e09fa-fd7a-4426-ae0c-4254aa73f3b9(intptr,intptr,int,intptr,intptr)

预先感谢

编辑1:

代码:

XAML:

<flv:FlowListView FlowColumnCount="1" SeparatorVisibility="Default" BackgroundColor="White" HasUnevenRows="True" VerticalOptions="FillAndExpand" 
                    FlowLoadingCommand="{Binding LoadUnreadNotificationsCommand}" FlowTotalRecords="{Binding TotalUnread}" FlowIsLoadingInfiniteEnabled="True"
                    FlowItemTappedCommand="{Binding NotificationSelectedCommand}" FlowIsLoadingInfinite="{Binding IsBusy}"
                    FlowItemsSource="{Binding UnreadNotifications}" IsPullToRefreshEnabled="True" IsRefreshing="{Binding IsBusy}" RefreshCommand="{Binding LoadUnreadNotificationsCommand}">

    <flv:FlowListView.FlowEmptyTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout Style="{StaticResource NoData}">
                    <controls:FontAwesomeSolidIcon Text="{x:Static core:Icon.Unread}" Style="{StaticResource IconDashboardLabel}" HorizontalOptions="Center" FontSize="20" TextColor="#cccccc" />
                    <Label Text="Não existem por ler" VerticalOptions="Center" HorizontalOptions="Center" TextColor="#cccccc" />
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </flv:FlowListView.FlowEmptyTemplate>

    <flv:FlowListView.FlowColumnTemplate>
        <DataTemplate>
            <list:Card />
        </DataTemplate>
    </flv:FlowListView.FlowColumnTemplate>

</flv:FlowListView>

C#:

public async Task LoadNotifications(bool read)
{


if (this.AcquireTapLock())
    {


var notificationsResponse = await ApiManager.GetNotifications(new PaginateNotificationsModel(Settings.IdUser, read, 0, Constants.ItemsByPage, string.Empty));

        if (notificationsResponse.IsSuccessStatusCode)
        {
            var response = await notificationsResponse.Content.ReadAsStringAsync();
            var result = await Task.Run(() => JsonConvert.DeserializeObject<ListNotificationResponseModel>(response));

            if (result.Status == ApiResponseStatus.Ok)
            {
                if (read)
                {
                    ReadNotifications = new ObservableRangeCollection<NotificationModel>(result.Notifications);
                    TotalRead = result.CountAfterFilter;
                }
                else
                {
                    UnreadNotifications = new ObservableRangeCollection<NotificationModel>(result.Notifications);
                    TotalUnread = result.CountAfterFilter;
                }
            }
        }

        IsBusy = false;
        this.ReleaseTapLock();
    }
}

1 个答案:

答案 0 :(得分:0)

最后我找到了原因,在FlowListView控件中将模式添加为OneWayToSource,以保持列表的总计数,这是一种合理的方法,如下所示:

FlowTotalRecords="{Binding yourViewmodel.Count, Mode=OneWayToSource}"