Image not shown for result suggestions in SearchBox
Asked Answered
P

1

6

I'm trying to use the SearchBox control introduced in Windows 8.1, but I can't figure out how to display the image in the result suggestions. The suggestions appear, but the space where the image should be remains blank:

enter image description here

Here's my XAML:

<SearchBox SuggestionsRequested="SearchBox_SuggestionsRequested" />

And my code behind:

    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();
        }
    }

Am I missing something?


Some extra details:

I tried to debug it with XAML Spy; each suggestion ListViewItem has its Content set to an instance of Windows.ApplicationModel.Search.Core.SearchSuggestion. On these SearchSuggestion objects, I noticed that the Text, Tag, DetailText, and ImageAlternateText properties are set to their correct value, but the Image property is null...


EDIT: So apparently AppendResultSuggestion accepts only an instance of RandomAccessStreamReference, not any other implementation of IRandomAccessStreamReference. I think this is a bug, since it's inconsistent with what is conveyed by the method signature. I filed it on Connect, please vote for it if you want it fixed!

Pumice answered 4/11, 2013 at 14:19 Comment(0)
A
5

The signature of AppendResultSuggestion calls for a IRandomAccessStreamReference:

public void AppendResultSuggestion(
    string text, string detailText, string tag, 
    IRandomAccessStreamReference image, 
    string imageAlternateText)

You can get it, if you already have a StorageFile (which you do) using CreateFromFile:

RandomAccessStreamReference.CreateFromFile(IStorageFile file)

But since you are starting with a URI, you might as well skip the extra step and use CreateFromUri:

RandomAccessStreamReference.CreateFromUri(Uri uri)

So you'd have something like:

var imageUri = new Uri("ms-appx:///test.png");
var imageRef = RandomAccessStreamReference.CreateFromUri(imageUri);
args.Request.SearchSuggestionCollection.AppendResultSuggestion("foo", "Details", "foo", imageRef, "Result")
Await answered 4/11, 2013 at 17:11 Comment(4)
Locating the cursor above StorageFile an pressing F12 key, shows the declaration of StorageFile. Apparently StorageFile already implements IRandomAccessStreamReference, why a different method is required?Calvinism
@Kiewic a very good question to which I do not have an answer. All I know is that not all implementations of IRandomAccessStreamReference are the same and that using RandomAccessStreamReference is supposed to be more "lightweight," so that's what I use (and well, it works ;) ). Maybe it should be a SO question…Await
It works, thanks!! I was sure it was supposed to work with StorageFile, since it implements IRandomAccessStreamReference. I had also tried to create my own implementation, but it didn't work either.Pumice
@madd0, I filed a bug on Connect, since the method signature implies that it should accept any implementation of the interface.Pumice

© 2022 - 2024 — McMap. All rights reserved.