我绑定了一个Web服务列表。第一次只加载5个项目,然后当用户滚动列表视图时,加载其他5个项目。每件事情都很好但我在一个场景中面临问题。 第一次绑定List时(不要滚动),如果我立即单击某个列表项,则会触发“ItemTepped”事件并将页面导航到其他页面但问题是它显示错误,如下图所示
我已将Active指示符用于加载。
此代码位于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表单中的线程。
答案 0 :(得分:0)
通常在您使用TemplateSelector
时发生,但为每行创建一个新的。 创建TemplateSelector
的一个实例并使用它。