使用 BackgroundWorker 自动完成功能不起作用
我正在使用 wpf 工具包中的 AutoCompleteBox,并由我自己实现填充 因为有很多数据,我想在后台线程中进行搜索。
这就是我搜索数字 12 时所想到的。 虽然它应该显示 4 个结果 - 12,120,121,122。
我做错了什么?
我尝试遵循的msdn指南: http://msdn.microsoft.com/en-us/library/system.windows.controls.autocompletebox.populate(VS.95).aspx
XAML:
<Grid>
<Controls:AutoCompleteBox x:Name="txtSearch" Populating="AutoCompleteBox_Populating" Height="30" Background="Beige" />
</Grid>
隐藏代码:
public partial class Window1 : Window {
private int MAX_NUM_OF_RESULTS = 3;
public List<Person> Persons { get; set; }
public List<string> Results { get; set; }
public Window1() {
InitializeComponent();
Persons = new List<Person>();
for (var i = 0; i < 1000000; i++) {
Persons.Add(new Person { Name = i.ToString() });
}
Results = new List<string>();
}
private void AutoCompleteBox_Populating(object sender, PopulatingEventArgs e) {
e.Cancel = true;
var b = new BackgroundWorker();
b.RunWorkerAsync(txtSearch.SearchText);
b.DoWork += b_DoWork;
b.RunWorkerCompleted += b_RunWorkerCompleted;
}
void b_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
txtSearch.ItemsSource = Results;
txtSearch.PopulateComplete();
}
void b_DoWork(object sender, DoWorkEventArgs e) {
Results.Clear();
var counter = 0;
foreach (var person in Persons) {
if (person.Name.StartsWith(e.Argument.ToString())) {
Results.Add(person.Name);
counter++;
if (counter > MAX_NUM_OF_RESULTS) {
break;
}
}
}
}
}
类 Person:
public class Person {
public string Name;
}
I'm using AutoCompleteBox from the wpf toolkit and I implement the populating by my own
since there is a lot of data and I want to do the search in a background thread.
this is what heppans when I search for the number 12.
while it should show me 4 results - 12,120,121,122.
What am I doing wrong ?
Guide on msdn that I tried to folow: http://msdn.microsoft.com/en-us/library/system.windows.controls.autocompletebox.populating(VS.95).aspx
XAML:
<Grid>
<Controls:AutoCompleteBox x:Name="txtSearch" Populating="AutoCompleteBox_Populating" Height="30" Background="Beige" />
</Grid>
Code behind:
public partial class Window1 : Window {
private int MAX_NUM_OF_RESULTS = 3;
public List<Person> Persons { get; set; }
public List<string> Results { get; set; }
public Window1() {
InitializeComponent();
Persons = new List<Person>();
for (var i = 0; i < 1000000; i++) {
Persons.Add(new Person { Name = i.ToString() });
}
Results = new List<string>();
}
private void AutoCompleteBox_Populating(object sender, PopulatingEventArgs e) {
e.Cancel = true;
var b = new BackgroundWorker();
b.RunWorkerAsync(txtSearch.SearchText);
b.DoWork += b_DoWork;
b.RunWorkerCompleted += b_RunWorkerCompleted;
}
void b_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
txtSearch.ItemsSource = Results;
txtSearch.PopulateComplete();
}
void b_DoWork(object sender, DoWorkEventArgs e) {
Results.Clear();
var counter = 0;
foreach (var person in Persons) {
if (person.Name.StartsWith(e.Argument.ToString())) {
Results.Add(person.Name);
counter++;
if (counter > MAX_NUM_OF_RESULTS) {
break;
}
}
}
}
}
Class Person:
public class Person {
public string Name;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将顺序更改为以下内容
Try change the order to the following
您确定您的搜索逻辑实际上正在执行吗?如果是这样,在将它们分配给 ItemsSource 之前,结果中是否有预期结果?
我认为:
应该是这样:
否则,您可能会面临让工作人员在设置事件处理程序之前启动的风险。
Are you certain your search logic is actually executing? If so, are the expected results in Results prior to assigning them to ItemsSource?
I think this:
Should be this:
Otherwise you risk having the worker start before setting up event handlers.