Question

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!

Was it helpful?

Solution

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")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top