首页 > 解决方案 > xamarin iOS c#SearchBar在ListView项目中搜索时卡住并且工作速度很慢

问题描述

使用:Visual Studio for Mac 上的 Xamarin

我有一个示例 SearchBar,其中包含来自数据库的已填充 ListView 和位置。当我启动应用程序时,我看到 ListView 中的位置,但是当我单击 SearchBar 并想从数据库中搜索某些项目时,SearchBar 卡住了。示例:我尝试输入“Plovdiv”.. 但我不能...只需输入 P ... 25 秒后搜索输入“l” ... 40 秒后我看到另一个“o” .. 并且 SearchBar 的工作速度非常缓慢以提示...

我的 MainPage.cs 代码:

 public partial class MainPage : ContentPage
{
    public string GlobalLat;
    public string GlobalLong;

    private MySqlConnection connection;
    private string server;
    private string database;
    private string uid;
    private string password;



    public MainPage()
    {
        InitializeComponent();

        listView.ItemsSource = GetContacts();
        this.BindingContext = this;

        Task task = GetUserLocationAsync();
    }

    public class Contacts
    {
        public string Place { get; set; }
    }

    IEnumerable<Contacts> GetContacts(string searchText = null)
    {
        server = "192.168.0.1,3306";
        database = "dbName";
        uid = "username";
        password = "";
        string connectionString;
        connectionString = "SERVER=" + server + ";" + "DATABASE=" +
        database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
        connection = new MySqlConnection(connectionString);
        connection.Open();
        var cmd = new MySqlCommand();
        cmd.Connection = connection;

        var contacts = new List<Contacts>();

        MySqlCommand command = new MySqlCommand($"SELECT place FROM places ORDER BY place", connection);

        using (MySqlDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                var place = new Contacts
                {
                    Place = reader[0].ToString()
                };

                contacts.Add(place);

            }
            connection.Close();
        }

        if (string.IsNullOrEmpty(searchText))
            return contacts;
        return contacts.Where(p => p.Place.StartsWith(searchText));
    }

    private void ListView_Refreshing(object sender, EventArgs e)
    {
        listView.ItemsSource = GetContacts();
        listView.EndRefresh();
    }
}
void SearchBar_TextChanged(System.Object sender, Xamarin.Forms.TextChangedEventArgs e)
    {
        listView.ItemsSource = GetContacts(e.NewTextValue);
    }

    void Btn_Search(System.Object sender, System.EventArgs e)
    {

    }
}

这是 MainPage.xaml 文件中的代码:

<StackLayout>  
            <SearchBar
               Placeholder="Enter city"
               TextChanged="SearchBar_TextChanged"
               SearchButtonPressed="Btn_Search"></SearchBar>  
            <ListView
                HeightRequest="250"
                x:Name="listView" >  
                <ListView.ItemTemplate>  
                    <DataTemplate>  
                        <TextCell Text="{Binding Place}">  
                    </TextCell>  
                    </DataTemplate>  
                </ListView.ItemTemplate>  
            </ListView>  
        </StackLayout>

所以搜索工作非常缓慢,每次我碰到她并想搜索时,他都会卡住。

SearchBar Stuck When Click on it

有没有办法解决这个问题?

标签: c#iosmysqlxamarinsearchbar

解决方案


first, keep a local list of Contacts, you do not need to refresh from the remote DB every time

List<Contact> data;

public MainPage()
{
    InitializeComponent();

    listView.ItemsSource = data = GetContacts();
    this.BindingContext = this;

    Task task = GetUserLocationAsync();
}

then, when you search, use the data you already have loaded, do NOT continually query the remote database

void SearchBar_TextChanged(System.Object sender, Xamarin.Forms.TextChangedEventArgs e)
{
    listView.ItemsSource = data.Where(p => p.Place.StartsWith(e.NewTextValue));
}

推荐阅读