【问题标题】:Java.Lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification (xamarin.forms)Java.Lang.IllegalStateException:适配器的内容已更改,但 ListView 未收到通知(xamarin.forms)
【发布时间】:2016-09-02 04:14:32
【问题描述】:

我已经从网络服务中绑定了一个列表。第一次只加载 5 个项目,然后当用户滚动列表视图时加载其他 5 个项目。每件事都运行良好,但我在一种情况下面临问题。 第一次绑定列表时(不要滚动),如果我立即点击了某个列表项,则会触发“ItemTepped”事件并且页面应该导航到其他页面,但问题是它向我显示错误,如下图所示.

我已经为加载设置了活动指示器。

此代码位于 xamarin.forms(便携式库)中。这不是 xamarin.Android 项目。

public partial class NewsGalleryListPage : ContentPage
    {
        int totalItems = 0;
        decimal MaxIndex = 0;
        int index = 0;
        List<Newslist> NewsList;
        public NewsGalleryListPage()
        {
            InitializeComponent();

            NewsGalleryLists.ItemTapped += NewsGalleryLists_ItemTapped;
            NewsGalleryLists.ItemAppearing += NewsGalleryLists_ItemAppearing1;
            loadingIndicator.IsVisible = true;
            loading.IsRunning = true;
            loading.IsVisible = true;
            CallWebServiceForNewsGalleryList(index);
        }

        private void NewsGalleryLists_ItemAppearing1(object sender, ItemVisibilityEventArgs e)
        {
            try
            {
                if (totalItems != 0)
                {
                    MaxIndex = Math.Ceiling(((decimal)totalItems) / 5);
                    if (index < MaxIndex)
                    {
                        if (NewsList != null && e.Item != null && e.Item == NewsList[NewsList.Count - 1])
                        {
                            index++;
                            if (index != MaxIndex)
                            {
                                loadingIndicator.IsVisible = true;
                                loading.IsVisible = true;
                                loading.IsRunning = true;
                                CallWebServiceForNewsGalleryList(index);
                            }
                        }
                    }
                }
            }
            catch(Exception ex)
            {

            }
        }

        private void NewsGalleryLists_ItemTapped(object sender, ItemTappedEventArgs e)
        {
            var selectedNewsGalleryListItem = sender as Xamarin.Forms.ListView;
            var obj = selectedNewsGalleryListItem.SelectedItem as CMO.ServicesClasses.Newslist;
            Navigation.PushAsync(new CMO.Gallery.NewsGalleryDetail(obj));
        }
        public async void CallWebServiceForNewsGalleryList(int index)
        {
            try
            {
                string lang = "en";

                if (Application.Current.Properties.ContainsKey("Language"))
                {
                    lang = Application.Current.Properties["Language"] as string;
                    // do something with i
                }
                List<KeyValuePair<string, string>> values = new List<KeyValuePair<string, string>>();
                values.Add(new KeyValuePair<string, string>("lang",lang));
                values.Add(new KeyValuePair<string, string>("title", ""));
                values.Add(new KeyValuePair<string, string>("index", Convert.ToString(index)));
                values.Add(new KeyValuePair<string, string>("limit", "5"));
                var response = await GeneralClass.GetResponse<CMO.ServicesClasses.RootObjectNewsGalleryList>("http://14.141.36.212/maharastracmo/api/getnewslist", values);
                if (response != null)
                {

                    if (NewsList == null || index==0)
                    {
                        NewsList = new List<Newslist>();
                    }
                    for(int i = 0; i < response.newslist.Count; i++)
                    {
                        var ObjectNewslist = new Newslist();
                        ObjectNewslist.page_id = response.newslist[i].page_id;
                        ObjectNewslist.title = response.newslist[i].title;
                        ObjectNewslist.date = response.newslist[i].date;
                        ObjectNewslist.news_photo = response.newslist[i].news_photo;
                        ObjectNewslist.content = response.newslist[i].content;
                        NewsList.Add(ObjectNewslist);
                    }
                    totalItems = response.total_results;
                    NewsGalleryLists.RowHeight = 100;
                    var x = NewsGalleryLists.RowHeight;
                    this.Title = AppResources.LNewsGallery;
                    NewsGalleryLists.ItemsSource = NewsList;
                }
            }
            catch (WebException exception)
            {
            }
            loading.IsVisible = false;
            loading.IsRunning = false;
            loadingIndicator.IsVisible = false;
        }
    }

我已经浏览了以下链接

https://forums.xamarin.com/discussion/23124/proper-way-to-update-the-tableviews-sections-content https://forums.xamarin.com/discussion/21994/threading-in-xamarin-forms

我试过了

Xamarin.Forms.Device.BeginInvokeOnMainThread (() =>
        {});

Task.Run(() => { });

作为初学者,我不知道如何处理 xamarin 表单中的线程。

【问题讨论】:

    标签: c# xamarin xamarin.android xamarin.forms xamarin-studio


    【解决方案1】:

    当您使用TemplateSelector 时通常会发生这种情况,但会为每一行创建一个新的。 创建一个TemplateSelector 的实例并使用它。

    【讨论】:

    • 我只有一个DataTemplate。为什么我们应该使用 DataTemplateSelector?我认为它的线程问题
    • 我几乎解决了这个问题。我只是将“ObservableCollection”替换为“List”。 INotifyChanged 属性在 ObservableCollection 中继承。但现在的问题是我的 ActiveIndicator(Loading) 没有显示。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-03
    • 2011-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多