首页 > 解决方案 > 将搜索词从 SearchHandler 传递到 Xamarin Forms 4 中的 ContentPage

问题描述

我正在尝试使用作为 Xamarin Forms 4 的一部分实现的新 SearchHandler。到目前为止,我发现获取建议非常容易,但现在我想引发一个事件,或者遵循建议的处理方法确认搜索。

public class FoodSearchHandler: SearchHandler
{
    IFoodDataStore dataStore = new FoodDataStore();

    protected override void OnQueryConfirmed()
    {
        base.OnQueryConfirmed();
        // What to do here?
    }

    protected override void OnQueryChanged(string oldValue, string newValue)
    {
        base.OnQueryChanged(oldValue, newValue);

        if(!string.IsNullOrWhiteSpace(newValue)
        {
            // Populate suggestions
            ItemsSource = dataStore.GetSuggestions(newValue);
        }
        else
        {
            ItemsSource = null;
        } 
    }
}

public partial class FoodsPage : ContentPage
{
    ObservableCollection<Food> Foods = new ObservableCollection<Food>();

    public ItemsPage()
    {
        InitializeComponent();

        // Wire up the search handler
        Shell.SetSearchHandler(this, new FoodSearchHandler());
        BindingContext = this;
    }
}

不幸的是,尽管alpha 文档提到了搜索处理程序,但它们没有包含有关如何使用它的任何详细信息,并且示例应用程序仅演示了填充建议。

有没有人可以提供关于我应该如何通知我的 ContentPage 我的 SearchHandler 确认搜索的指针?

标签: xamarin.formsxamarin-forms-4

解决方案


因此,在阅读了更多 Shell 文档之后,似乎我在这种情况下想要做的是使用 Shell 的新导航并导航到将搜索文本作为查询传递的路由,例如:

protected override void OnQueryConfirmed()
{
    base.OnQueryConfirmed();

    var shell = Application.Current.MainPage as Shell;
    shell.GoToAsync($"app:///fructika/search?query={Query}", true);
}

注意:现在传递数据似乎不起作用,或者如果确实如此,我做错了,但我会提出一个单独的问题。


推荐阅读