Xamarin在其中键入文本时,在列表视图弹出窗口中形成带有建议项的条目 [英] Xamarin forms Entry with suggestion items in a listview popup when typing the text in it

查看:43
本文介绍了Xamarin在其中键入文本时,在列表视图弹出窗口中形成带有建议项的条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Entry字段.还有我要从数据库中获取项目的列表.

I have an Entry field. And a list of items which I'm fetching it from Database.

在输入带有DB中的那些条目的Entry时,我需要显示列表弹出窗口,并根据字符串过滤这些条目.

I need to show the list popup when typing in the Entry with those items from DB and filter those based on the string.

如果用户在列表中找到该名称,则可以选择该名称,该名称应显示在条目"中,后跟空格,并且用户可以再输入一个字符串.(类似于在Gmail中输入电子邮件地址).如果用户未在列表中找到该名称,则可以键入特定名称,并在空格后输入新的字符串.

If the user finds the name in the list, he can select the one and it should display in the Entry followed by space and the user can enter one more string. (similar to entering email addresses in Gmail). If the user doesn't find the name in the list, he can type the particular name and after hitting space he should be able to enter the new string.

我发现SyncFusion有nuget packgae.我们可以不使用任何第三方库就能实现这一目标吗?

I found there is nuget packgae from SyncFusion. Can we achieve this without using any third party libs?

推荐答案

您可以将listview放在条目的下方:

You can place listview below of the entry:

在XAML中:

<Grid>
<Grid.RowDefinitions>
    <RowDefinition Height="40" />
    <RowDefinition Height="40" />                    
</Grid.RowDefinitions>    
        <Entry
            x:Name="entryMain"
            Grid.Row="0"
            TextChanged="OnEntryChanged" />
        <ListView
            x:Name="lstSuggest"
            Grid.Row="1"
            IsVisible="False"
            ItemTapped="ItemSelected"
            SeparatorVisibility="None" />
</Grid>

后面的代码:

lstSuggest.ItemsSource = _suggestion;

private void OnEntryChanged(object sender, TextChangedEventArgs e)
{            
    if (entryMain.Text != null && lstSuggest.ItemsSource != null)
    {                  
        if (_suggestion.Any(x=> x.StartsWith(e.NewTextValue)) && entryMain.Text != string.Empty)
        {
            var items = new List<string>();

            foreach (var item in _suggestion.FindAll(x => x.StartsWith(e.NewTextValue)))
            {
                items.Add(item);
            }

            lstSuggest.ItemsSource = items;
            lstSuggest.IsVisible = true;
        }
        else
        { 
            lstSuggest.IsVisible = false;
        }
    }
}

private void ItemSelected(object sender, EventArgs args)
{
    if (((ListView)sender).SelectedItem == null)
        return;

    entryMain.Text = lstSuggest.SelectedItem.ToString();
    ((ListView)sender).SelectedItem = null;
    lstSuggest.IsVisible = false;
}

此外,如果要删除listview水平滚动条的可见性,则可以创建自定义控件.

In addition, if you want remove listview horizontal scrollbar visibility, you can create custom control.

奖金:Android默认 https://docs.microsoft.com/tr-tr/xamarin/android/user-interface/controls/auto-complete

Bonus: Android default https://docs.microsoft.com/tr-tr/xamarin/android/user-interface/controls/auto-complete

这篇关于Xamarin在其中键入文本时,在列表视图弹出窗口中形成带有建议项的条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆