使用 BackgroundWorker 自动完成功能不起作用

发布于 2024-10-10 04:20:42 字数 2137 浏览 7 评论 0原文

我正在使用 wpf 工具包中的 AutoCompleteBox,并由我自己实现填充 因为有很多数据,我想在后台线程中进行搜索。

这就是我搜索数字 12 时所想到的。 虽然它应该显示 4 个结果 - 12,120,121,122。

我做错了什么?

我尝试遵循的msdn指南: http://msdn.microsoft.com/en-us/library/system.windows.controls.autocompletebox.populate(VS.95).aspx

alt text

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

alt text

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尐籹人 2024-10-17 04:20:42

尝试将顺序更改为以下内容

var b = new BackgroundWorker();
b.DoWork += b_DoWork;
b.RunWorkerCompleted += b_RunWorkerCompleted;
b.RunWorkerAsync(txtSearch.SearchText);

Try change the order to the following

var b = new BackgroundWorker();
b.DoWork += b_DoWork;
b.RunWorkerCompleted += b_RunWorkerCompleted;
b.RunWorkerAsync(txtSearch.SearchText);
爱的十字路口 2024-10-17 04:20:42

您确定您的搜索逻辑实际上正在执行吗?如果是这样,在将它们分配给 ItemsSource 之前,结果中是否有预期结果?

我认为:

    var b = new BackgroundWorker();
    b.RunWorkerAsync(txtSearch.SearchText);
    b.DoWork += b_DoWork;
    b.RunWorkerCompleted += b_RunWorkerCompleted;

应该是这样:

    var b = new BackgroundWorker();
    b.DoWork += b_DoWork;
    b.RunWorkerCompleted += b_RunWorkerCompleted;
    b.RunWorkerAsync(txtSearch.SearchText);

否则,您可能会面临让工作人员在设置事件处理程序之前启动的风险。

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:

    var b = new BackgroundWorker();
    b.RunWorkerAsync(txtSearch.SearchText);
    b.DoWork += b_DoWork;
    b.RunWorkerCompleted += b_RunWorkerCompleted;

Should be this:

    var b = new BackgroundWorker();
    b.DoWork += b_DoWork;
    b.RunWorkerCompleted += b_RunWorkerCompleted;
    b.RunWorkerAsync(txtSearch.SearchText);

Otherwise you risk having the worker start before setting up event handlers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文