gpt4 book ai didi

c# - 如何通过使用异步方法加载结果来在 Windows 8.1 中使用 SearchBox

转载 作者:太空狗 更新时间:2023-10-30 00:40:48 25 4
gpt4 key购买 nike

我正在使用 SearchBox 列出从服务器获取的一些项目。对服务器的调用是在异步方法中发生的。

我遇到了异常发生“System.InvalidOperationException”类型的异常WinRT 信息:在意外时间调用了方法。

我的 XAML

<SearchBox Name="SearchBox"
Style="{StaticResource AccountSearchBoxStyle}"
Grid.Row="1"
Margin="120,0,0,0"
HorizontalAlignment="Left"
SuggestionsRequested="SearchBox_SuggestionsRequested"
SearchHistoryEnabled="False" > </SearchBox>

我的代码在后面

private async void SearchBox_SuggestionsRequested(SearchBox sender,
SearchBoxSuggestionsRequestedEventArgs args){
if (string.IsNullOrEmpty(args.QueryText))
{
return;
}
var collection = args.Request.SearchSuggestionCollection;
if(oldquery != args.QueryText)
{
var listOfBanks = await addFIPageViewModel.GetBanksOnQuery();
foreach (Institution insti in listOfBanks)
{
collection.AppendQuerySuggestion(insti.name);
}
oldquery = args.QueryText;
}}

最佳答案

MSDN 可以提供更多关于此的明确信息。

花时间后我偶然发现了this blog并找到了答案

后面的代码需要修改如下。

private async void SearchBox_SuggestionsRequested(SearchBox sender,
SearchBoxSuggestionsRequestedEventArgs args){
if (string.IsNullOrEmpty(args.QueryText))
{
return;
}
var collection = args.Request.SearchSuggestionCollection;
if(oldquery != args.QueryText)
{
//ADD THIS LINE
var deferral = args.Request.GetDeferral();

var listOfBanks = await addFIPageViewModel.GetBanksOnQuery();
foreach (Institution insti in listOfBanks)
{
collection.AppendQuerySuggestion(insti.name);
}

//ADD THIS LINE
deferral.Complete();

oldquery = args.QueryText;
}}

关于c# - 如何通过使用异步方法加载结果来在 Windows 8.1 中使用 SearchBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24413748/

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