gpt4 book ai didi

c# - 如何不断延迟方法的执行

转载 作者:行者123 更新时间:2023-11-30 20:43:10 24 4
gpt4 key购买 nike

我有一个名为“CompanyView”的 ICollectionVIew

我还有一个名为“CompanyFilter”的过滤器。

还有一个绑定(bind)到“SearchCompanyTitle”属性的 Textbox

当我在数据绑定(bind) 文本框 中输入时,“CompanyFilter”会随着每个字母被触发,并且“CompanyView”会被过滤以显示相关信息结果。

这很好。

不幸的是,我正在过滤的表格有大约 9000 行,因此在您按下键盘上的键和它出现在屏幕上之间往往会有明显的延迟。

所以我决定改为确保过滤器在用户完成输入时自动触发。这就提出了一个问题,即 ViewModel 如何知道用户何时完成?

下面是我做的;

    // This is the property the Textbox is bound to
private string _searchCompanyTitle = "";
public string SearchCompanyTitle
{
get { return _searchCompanyTitle; }
set
{
_searchCompanyTitle = value;
OnPropertyChanged("SearchCompanyTitle");

// After a character has been typed it will fire the below method
SearchCompany();

}
}

// This method is fired by the above property everytime a character is typed into the textbox
// What this method is meant to do is wait 1000 microseconds before it fires the filter
// However I need the timer to be reset every time a character is typed,
// Even if it hasn't reached 1000 yet
// But it doesn't do that. It continues to count before triggering the filter
private async void SearchCompany()
{
bool wait = true;

while (wait == true)
{
await Task.Delay(1000);

wait = false;
}

CompanyView.Filter = CompanyFilter;
}

// And this is the filter
private bool CompanyFilter(object item)
{
company company = item as company;

return company.title.Contains(SearchCompanyTitle);
}

这就是我的问题。我需要过滤器仅在计时器达到 1000 时触发,而不是之前触发。同时,每次属性触发该方法时,我都需要计时器返回 0。显然我做的不对。有什么想法吗?

最佳答案

听起来像是绑定(bind)的完美候选 Delay :

<TextBox Text="{Binding SearchCompanyTitle, Delay=1000}"/>

关于c# - 如何不断延迟方法的执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30777182/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com