【问题标题】:Xamarin.Form FlowListView - NullReferenceException when refresh the listXamarin.Form FlowListView - 刷新列表时出现 NullReferenceException
【发布时间】:2019-04-24 15:01:52
【问题描述】:

重现步骤:

从数据库或其他来源加载集合 从数据库中删除项目 例如,使用 Pull-to-Refresh 刷新列表 结果: 应用崩溃。

观察:

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

版本:

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] in 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 插入) [0x00000] 在 D:\a\1\s\Xamarin.Forms.Core\TemplatedItemsList.cs:543 11-20 23:38:07.111 I/MonoDroid(10001):在 Xamarin.Forms.Internals.TemplatedItemsList2[TView,TItem].GetOrCreateContent (System.Int32 索引,System.Object 项)[0x00023] in 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] in 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 转换视图,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] 在 :0 11-20 23:38:07.111 I/MonoDroid(10001):在(包装动态方法) 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();
    }
}

【问题讨论】:

  • Hrm,我想我会赞成一个格式很好的问题。干得好,但是有些代码会很好
  • @TheGeneral 我添加了一些代码。谢谢

标签: c# xamarin.forms


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-12
    相关资源
    最近更新 更多