【问题标题】:Showing search options on click for Xamarin Forms SearchBar单击 Xamarin Forms SearchBar 时显示搜索选项
【发布时间】:2020-02-07 11:36:25
【问题描述】:

有什么方法可以在 Xamarin Forms 中使用 SearchBar 并使其一旦单击它,它将自动选择显示我选择的建议作为下拉菜单,然后单击建议后,它会用文本填充搜索栏吗?

看起来像这样:

但主要区别在于用户不必输入 r 即可获得建议。只要用户点击搜索栏,建议就会出现。

谢谢

【问题讨论】:

  • 您可以在 SeachBar 下方添加一个 StackLayout,它会在您搜索后显示您的建议。由于 XF 不提供此类功能,因此您必须自己构建某种建议视图
  • 所以我的代码看起来像:<StackLayout> <SearchBar Placeholder="Search" Text="{Binding SearchText}"/> (Some kind of suggestion here) </StackLayout>

标签: xamarin.forms


【解决方案1】:

因此,您可以将您的建议添加到仅当其中有项目时才可见的集合中。一旦用户键入了一些值得建议的选项,您将添加到此集合中。如果用户选择一个选项或继续输入,您需要清除/更新集合。

代码

ObservableCollection<SuggestionViewModel> SuggestionCollection { get; } = new ObservableCollection<SuggestionViewModel>(); 


public SuggestionViewModel
{
   public string Name {get; set;}
}

XAML

<StackLayout> 
    <SearchBar Placeholder="Search" Text="{Binding SearchText}"/> 
    <StackLayout BindableLayout.ItemsSource="{Binding SuggestionCollection}"
                 Orientation="Vertical">
        <BindableLayout.ItemTemplate>
            <DataTemplate>
                <Label Text={Binding Name}>
            </DataTemplate>
        </BindableLayout.ItemTemplate>
    </StackLayout>
</StackLayout> 

您需要用您的 ContentView 将 xaml 部分“覆盖”,这样建议 stacklayout 就不会干扰您的初始 ContentView 布局。像这样的

<Grid>
   <StackLayout>
   </StackLayout>
   <ContentView>
   </ContentView>
</Grid>

【讨论】:

    猜你喜欢
    • 2018-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-13
    • 2017-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多