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!

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758

1 Answers1

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")
madd0
  • 9,053
  • 3
  • 35
  • 62
  • 2
    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? – kiewic Nov 04 '13 at 17:21
  • 1
    @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… – madd0 Nov 04 '13 at 17:36
  • 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. – Thomas Levesque Nov 04 '13 at 17:42
  • @madd0, I filed [a bug on Connect](https://connect.microsoft.com/VisualStudio/feedback/details/807704/searchbox-windows-8-1-doesnt-show-the-image-if-it-is-not-an-instance-of-randomaccessstreamreference), since the method signature implies that it should accept any implementation of the interface. – Thomas Levesque Nov 04 '13 at 22:26