c# - 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>
所以搜索工作非常缓慢,每次我碰到她并想搜索时,他都会卡住。
有没有办法解决这个问题?
解决方案
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));
}
推荐阅读
- python - TypeError:MergeFrom() 的参数必须是同一类的实例:预期的 TensorShapeProto 得到了 TensorShapeProto。在 tf.keras.layers.Embedding
- html - 使用烧瓶和 SQLite 创建用户可编辑的表单
- bash - 如何使用 bash 脚本自动进行远程登录
- html - 在表格内对齐表格
- php - mysqli 更好的方法来改变使用什么列?
- asp.net - 如何为无限 WebSocket 连接配置 IIS 和 ASP.NET?
- reactjs - 如何在放大构建脚本中包含 aws-export 文件
- wpf - xaml 样式 - 如何在键上更改颜色
- amazon-redshift - 无法通过 Palto Alto 连接到 AWS Redshift
- jquery - 调整两个 iframe 的大小会失去对鼠标拖动的关注