我尝试使用Windows 8.1中引入的SearchBox
控件,但我无法弄清楚如何在结果建议中显示图像。建议出现,但图像应该留在空白处:
这是我的XAML:
<SearchBox SuggestionsRequested="SearchBox_SuggestionsRequested" />
我的代码背后:
private async void SearchBox_SuggestionsRequested(SearchBox sender, SearchBoxSuggestionsRequestedEventArgs args)
{
var deferral = args.Request.GetDeferral();
try
{
var imageUri = new Uri("ms-appx:///test.png");
var imageRef = await StorageFile.GetFileFromApplicationUriAsync(imageUri);
args.Request.SearchSuggestionCollection.AppendQuerySuggestion("test");
args.Request.SearchSuggestionCollection.AppendSearchSeparator("Foo Bar");
args.Request.SearchSuggestionCollection.AppendResultSuggestion("foo", "Details", "foo", imageRef, "Result");
args.Request.SearchSuggestionCollection.AppendResultSuggestion("bar", "Details", "bar", imageRef, "Result");
args.Request.SearchSuggestionCollection.AppendResultSuggestion("baz", "Details", "baz", imageRef, "Result");
}
finally
{
deferral.Complete();
}
}
我错过了什么吗?
一些额外的细节:
我尝试用XAML Spy调试它;每个建议ListViewItem
都将Content
设置为Windows.ApplicationModel.Search.Core.SearchSuggestion
的实例。在这些SearchSuggestion
个对象上,我注意到Text
,Tag
,DetailText
和ImageAlternateText
属性设置为正确的值,但是{{1 property is null ...
编辑:显然Image
只接受AppendResultSuggestion
的实例,而不是RandomAccessStreamReference
的任何其他实现。我认为这是一个错误,因为它与方法签名所传达的内容不一致。我filed it on Connect,如果您想要修复它,请投票支持!
答案 0 :(得分:5)
AppendResultSuggestion
的签名要求IRandomAccessStreamReference
:
public void AppendResultSuggestion(
string text, string detailText, string tag,
IRandomAccessStreamReference image,
string imageAlternateText)
如果您使用CreateFromFile
已经拥有StorageFile
(您可以),则可以获得
RandomAccessStreamReference.CreateFromFile(IStorageFile file)
但是,既然你是从一个URI开始的,那么你可以跳过额外的步骤并使用CreateFromUri
:
RandomAccessStreamReference.CreateFromUri(Uri uri)
所以你有类似的东西:
var imageUri = new Uri("ms-appx:///test.png");
var imageRef = RandomAccessStreamReference.CreateFromUri(imageUri);
args.Request.SearchSuggestionCollection.AppendResultSuggestion("foo", "Details", "foo", imageRef, "Result")