xamarin iOS 在 ListView 项中搜索时,c# SearchBar 卡住并且工作超慢
xamarin iOS c# SearchBar stuck and work hyper slowly when Search in ListView items
使用:Xamarin on Visual Studio for Mac
我有一个示例 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>
所以搜索工作非常缓慢,每次我点击她并想要搜索时他都会卡住..
有办法解决这个问题吗?
首先,保存一份本地联系人列表,不需要每次都从远程数据库刷新
List<Contact> data;
public MainPage()
{
InitializeComponent();
listView.ItemsSource = data = GetContacts();
this.BindingContext = this;
Task task = GetUserLocationAsync();
}
然后,搜索时,使用已经加载的数据,不要继续查询远程数据库
void SearchBar_TextChanged(System.Object sender, Xamarin.Forms.TextChangedEventArgs e)
{
listView.ItemsSource = data.Where(p => p.Place.StartsWith(e.NewTextValue));
}
使用:Xamarin on Visual Studio for Mac
我有一个示例 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>
所以搜索工作非常缓慢,每次我点击她并想要搜索时他都会卡住..
有办法解决这个问题吗?
首先,保存一份本地联系人列表,不需要每次都从远程数据库刷新
List<Contact> data;
public MainPage()
{
InitializeComponent();
listView.ItemsSource = data = GetContacts();
this.BindingContext = this;
Task task = GetUserLocationAsync();
}
然后,搜索时,使用已经加载的数据,不要继续查询远程数据库
void SearchBar_TextChanged(System.Object sender, Xamarin.Forms.TextChangedEventArgs e)
{
listView.ItemsSource = data.Where(p => p.Place.StartsWith(e.NewTextValue));
}