如何列出Xamarin项目中的后50个客户

时间:2019-05-01 12:06:45

标签: c# xamarin

我的xamarin项目在listview的Android端工作。但是,IOS不能正常运行。我收到并在Web服务上列出从1到50的客户列表。但是,当列出第二50个客户时,滚动条将保持在列表顶部,并保持在不同位置。我在寻找解决方案,但没有找到。请帮助。

    public partial class MusteriListesi : BasePage
{
    ListView listView;
    int bas;
    int bit;
    bool isLoading;
    public string mKodu = "";
    string mFirma = "";
    string mSehir = "";
    string mIlce = "";
    string mSemt = "";
    string mAdres = "";
    public string mCkp = "";
    public List<Musteriler> Items { get; set; }
    public MusteriListesi(string kodu, string firma, string sehir, string ilce, string semt, string adres,string ckp)
    {
        InitializeComponent();
        mKodu = kodu;mFirma = firma;mSehir = sehir;mIlce = ilce;mSemt = semt;mAdres = adres;mCkp = ckp;
        if (mCkp == "P")
        {
            this.Title = "Aday Listesi";
        }
        bas = 1;
        bit = 50;
        if (String.IsNullOrEmpty(mSehir))
        {
            mSehir = "Tüm Şehirler";
        }
        if (String.IsNullOrEmpty(mIlce))
        {
            mIlce = "Tüm İlçeler";
        }
        if (String.IsNullOrEmpty(mSemt))
        {
            mSemt = "Tüm Semtler";
        }
        if (String.IsNullOrEmpty(mKodu))
        {
            mKodu = "";
        }
        if (String.IsNullOrEmpty(mFirma))
        {
            mFirma = "";
        }
        if (String.IsNullOrEmpty(mAdres))
        {
            mAdres = "";
        }
        listView = MyListView;
        Items = new List<Musteriler>();
        Mobil.webService.KartServis service = new Mobil.webService.KartServis()
        {
            Url = Application.Current.Properties["servisyolu"].ToString()
        };
        List<Mobil.webService.Musteriler> MusteriLists = new List<Mobil.webService.Musteriler>(service.MusteriListele(mCkp, bas, bit, mKodu, mFirma, mSehir, mSemt, mIlce, mAdres, Application.Current.Properties["lisans"].ToString(), Application.Current.Properties["KNO"].ToString()));
        foreach (Mobil.webService.Musteriler items in MusteriLists)
        {
            Items.Add(new Musteriler { Kodu = items.Kodu, Firma = items.Firma, Adres = items.Adres, Telefon = items.Telefon, Sayi = items.Sayi });
        }
        listView.ItemsSource = Items;
    }
    async void Handle_ItemTapped(object sender, Xamarin.Forms.ItemTappedEventArgs e)
    {
        var item = (Musteriler)e.Item;
        if (item == null)
        {
            return;
        }
        await Navigation.PushAsync(new MusteriDetay(item.Kodu));
        ((ListView)sender).SelectedItem = null;
    }
    protected void Handle_ItemAppearing(object sender, Xamarin.Forms.ItemVisibilityEventArgs e)
    {
        if(isLoading || Items.Count == 0)
        {
            return;
        }
        if (e.Item == Items[Items.Count - 1])
        {
            LoadItems();
        }
    }
    public async Task LoadItems()
    {
        isLoading = true;
        listView.ItemsSource = null;
        bas = bas + 50;
        bit = bit + 50;
        Mobil.webService.KartServis service = new Mobil.webService.KartServis()
        {
            Url = Application.Current.Properties["servisyolu"].ToString()
        };
        List<Mobil.webService.Musteriler> MusteriLists = new List<Mobil.webService.Musteriler>(service.MusteriListele(mCkp, bas, bit, mKodu, mFirma, mSehir, mSemt, mIlce, mAdres, Application.Current.Properties["lisans"].ToString(), Application.Current.Properties["KNO"].ToString()));
        if (MusteriLists.Count > 0)
        {
            Device.StartTimer(TimeSpan.FromSeconds(1), () =>
            {
                Show();
                foreach (Mobil.webService.Musteriler items in MusteriLists)
                {
                    Items.Add(new Musteriler { Kodu = items.Kodu, Firma = items.Firma, Adres = items.Adres, Telefon = items.Telefon, Sayi = items.Sayi });
                }
                listView.ItemsSource = Items;
                listView.ScrollTo(Items[bas], ScrollToPosition.Start, false);
                isLoading = false;
                Hide();
                return false;
            });
        }
        else
        {
            isLoading = false;
        }
    }

1 个答案:

答案 0 :(得分:0)

  

但是,当列出第二50个客户时,滚动条将保持在列表顶部,并保持在不同位置。

原因之一:下一项没有退出。在下面的代码中,应确保Items在运行下面的代码后有100个数据。

foreach (Mobil.webService.Customer items in CustomerList)
    {
        Items.Add(new Customer { Kodu = items.code, City = items.City });
    }

原因二:运行代码时,需要更多时间来完成它,您应该添加计时器或异步任务来完成它。然后继续其余步骤。

解决方案示例:测试将本地数据添加到Items,并添加计时器以完成它。

private async Task LoadItems()
{
    isLoading = true;
    Title = "Loading";

    //simulator delayed load
    Device.StartTimer(TimeSpan.FromSeconds(2), () =>
    {
        for (int i = 0; i < 50; i++)
        {
            Items.Add(string.Format("Item {0}", Items.Count));
        }
        Title = "Done";
        isLoading = false;
        listview.ScrollTo(Items[Items.Count - 1], ScrollToPosition.Start, false);
        //stop timer
        return false;
    });
}

-------------------------------------------------- ---------------更新---------------------------------- --------------------------

解决方案:从共享方法 LoadItems 中,问题出在以下代码中;

listView.ScrollTo(listView.SelectedItem, ScrollToPosition.MakeVisible, false);

您应该删除代码,因为导致无限循环的LoadItems方法的触发器每次在代码中都加载了+100,程序界面很快就会卡住了。

    public async void LoadItems()
    {
        isLoading = true;
        Show();

        listView.ItemsSource = null;
        bas = bas + 50;
        bit = bit + 50;


          Mobil.webService.KartServis service = new Mobil.webService.KartServis()
          {
             Url = Application.Current.Properties["servisyolu"].ToString()
          };
          List<Mobil.webService.Musteriler> MusteriLists = new List<Mobil.webService.Musteriler>(service.MusteriListele("K", bas, bit, mKodu, mFirma, mSehir, mSemt, mIlce, mAdres, Application.Current.Properties["lisans"].ToString(), Application.Current.Properties["KNO"].ToString()));
          foreach (Mobil.webService.Musteriler items in MusteriLists)
          {
              Items.Add(new Musteriler { Kodu = items.Kodu, Firma = items.Firma,   Adres = items.Adres, Telefon = items.Telefon, Sayi = items.Sayi });
          }
          for (int i = 0; i < Items.Count; i++)
          {
            Console.WriteLine(Items[i].Sayi);
          }
          var lastItem = Items[1];
          listView.ItemsSource = Items;
          isLoading = false;

          listView.SelectedItem = lastItem;
          //listView.ScrollTo(listView.SelectedItem, ScrollToPosition.MakeVisible, false);
          Hide();

    }

如果不删除,则应添加一个Timer来检查它是否在循环添加项目。下面的代码将循环添加项目,这可能不是一个好逻辑。

public async void LoadItems()
    {
        isLoading = true;
        Show();

        listView.ItemsSource = null;
        bas = bas + 50;
        bit = bit + 50;

        Device.StartTimer(TimeSpan.FromSeconds(2), () =>
        {
          Mobil.webService.KartServis service = new Mobil.webService.KartServis()
          {
             Url = Application.Current.Properties["servisyolu"].ToString()
          };
          List<Mobil.webService.Musteriler> MusteriLists = new List<Mobil.webService.Musteriler>(service.MusteriListele("K", bas, bit, mKodu, mFirma, mSehir, mSemt, mIlce, mAdres, Application.Current.Properties["lisans"].ToString(), Application.Current.Properties["KNO"].ToString()));
          foreach (Mobil.webService.Musteriler items in MusteriLists)
          {
              Items.Add(new Musteriler { Kodu = items.Kodu, Firma = items.Firma,   Adres = items.Adres, Telefon = items.Telefon, Sayi = items.Sayi });
          }
          for (int i = 0; i < Items.Count; i++)
          {
            Console.WriteLine(Items[i].Sayi);
          }
          var lastItem = Items[1];
          listView.ItemsSource = Items;
          isLoading = false;

          listView.SelectedItem = lastItem;
          listView.ScrollTo(listView.SelectedItem, ScrollToPosition.MakeVisible, false);
          Hide();
        });
    }

注意:如果该Api每次获得50件物品,则没有其他问题。

  

列表MusteriLists =新列表(service.MusteriListele(“ K”,bas,bit,mKodu,mFirma,mSehir,mSemt,mIlce,mAdres,Application.Current.Properties [“ lisans”]。ToString(),应用程序。 Current.Properties [“ KNO”]。ToString()));

如果不是每次都达到50,则应在 LoadItems 中设置Items = new List<Musteriler>();。然后Itmes.count将为50,100,150,等等,否则为50,150,300,等等。这将产生一个项目数量意外。