【问题标题】:Passing the search term from SearchHandler to ContentPage in Xamarin Forms 4将搜索项从 SearchHandler 传递到 Xamarin Forms 4 中的 ContentPage
【发布时间】:2018-12-25 21:56:26
【问题描述】:

我正在尝试使用作为 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 docs 提到了搜索处理程序,但它们并未包含有关如何使用它的任何详细信息,并且示例应用程序仅演示了填充建议。

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

【问题讨论】:

  • 感谢@LucasZhang-MSFT,但我没有看到该示例中正在处理搜索确认事件。它只是调用基类方法。是否发生了一些神奇的约定处理?

标签: xamarin.forms xamarin-forms-4


【解决方案1】:

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

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

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

注意passing data 现在看起来不行,或者如果我做错了,但我会提出一个单独的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-25
    • 2020-09-16
    相关资源
    最近更新 更多