搜索框中的结果建议未显示图像

时间:2013-11-04 14:19:01

标签: c# xaml windows-runtime windows-8.1 search-box

我尝试使用Windows 8.1中引入的SearchBox控件,但我无法弄清楚如何在结果建议中显示图像。建议出现,但图像应该留在空白处:

enter image description here

这是我的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个对象上,我注意到TextTagDetailTextImageAlternateText属性设置为正确的值,但是{{1 property is null ...


编辑:显然Image只接受AppendResultSuggestion的实例,而不是RandomAccessStreamReference的任何其他实现。我认为这是一个错误,因为它与方法签名所传达的内容不一致。我filed it on Connect,如果您想要修复它,请投票支持!

1 个答案:

答案 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")
相关问题