【问题标题】:Image not shown for result suggestions in SearchBox图片未在搜索框中显示结果建议
【发布时间】:2013-11-04 14:19:01
【问题描述】:

我正在尝试使用 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 属性设置为正确的值,但Image 属性为空...


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

【问题讨论】:

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


    【解决方案1】:

    AppendResultSuggestion 的签名要求IRandomAccessStreamReference:

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

    如果你已经有一个StorageFile(你有),你可以使用CreateFromFile:

    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")
    

    【讨论】:

    • 将光标定位在StorageFile上方并按下F12键,显示StorageFile的声明。显然StorageFile 已经实现了IRandomAccessStreamReference,为什么需要不同的方法?
    • @Kiewic 一个非常好的问题,我没有答案。我所知道的是,并非所有IRandomAccessStreamReference 的实现都是相同的,并且使用RandomAccessStreamReference 应该更“轻量级”,所以这就是我使用的(而且,它有效;))。也许这应该是一个 SO 问题……
    • 它有效,谢谢!!我确信它应该适用于StorageFile,因为它实现了IRandomAccessStreamReference。我也尝试过创建自己的实现,但也没有用。
    • @madd0,我提交了a bug on Connect,因为方法签名暗示它应该接受接口的任何实现。
    猜你喜欢
    • 1970-01-01
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多