如何通过使用异步方法加载结果,以使用搜索盒在Windows 8.1 [英] How to use SearchBox in Windows 8.1 by loading the results using a async method
问题描述
我使用一个搜索盒,列出从服务器获取一些项目。
到服务器的调用是发生在一个异步方法。
我得到一个异常
'System.InvalidOperationException'类型的异常发生
WinRT的信息:一种方法被称为在一个意想不到的时间
我的XAML
<搜索盒名称=搜索盒
风格={StaticResource的AccountSearchBoxStyle}
Grid.Row =1
保证金=120,0,0 ,0
的HorizontalAlignment =左
SuggestionsRequested =SearchBox_SuggestionsRequested
SearchHistoryEnabled =FALSE> < /搜索盒>
我的身后
码
专用异步无效SearchBox_SuggestionsRequested(搜索盒发件人,
SearchBoxSuggestionsRequestedEventArgs参数){
如果(string.IsNullOrEmpty(args.QueryText))
{
的回报;
}
变种集合= args.Request.SearchSuggestionCollection;
如果(oldquery = args.QueryText!)
{
VAR listOfBanks =等待addFIPageViewModel.GetBanksOnQuery();
的foreach(机构insti在listOfBanks)
{
collection.AppendQuerySuggestion(insti.name);
}
oldquery = args.QueryText;
}}
MSDN本来可以多提供这个明确的信息。
花时间,我偶然发现的这个博客并找到了答案
后面的代码需要如下修改。
专用异步无效SearchBox_SuggestionsRequested(搜索盒发件人,
SearchBoxSuggestionsRequestedEventArgs参数){
如果(string.IsNullOrEmpty( args.QueryText))
{
的回报;
}
变种集合= args.Request.SearchSuggestionCollection;
如果(oldquery = args.QueryText!)
{
//加入这一行
VAR延期= args.Request.GetDeferral();
VAR listOfBanks =等待addFIPageViewModel.GetBanksOnQuery();
的foreach(机构insti在listOfBanks)
{
collection.AppendQuerySuggestion(insti.name);
}
//加入这一行
deferral.Complete();
oldquery = args.QueryText;
}
I am using a SearchBox to list some items that are obtained from the server. The call to the server is happening in a async method.
I am getting an exception An exception of type 'System.InvalidOperationException' occurred WinRT information: A method was called at an unexpected time.
My XAML
<SearchBox Name="SearchBox"
Style="{StaticResource AccountSearchBoxStyle}"
Grid.Row="1"
Margin="120,0,0,0"
HorizontalAlignment="Left"
SuggestionsRequested="SearchBox_SuggestionsRequested"
SearchHistoryEnabled="False" > </SearchBox>
My code behind
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 could have provided much clear information about this.
After spending time I stumbled upon this blog and found the answer
The code behind needs to be modified as follows.
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;
}}
这篇关于如何通过使用异步方法加载结果,以使用搜索盒在Windows 8.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!