我使用以下代码创建了一个使用Xamarin.Forms.的搜索栏
但它在这一点上断裂了。
protected void Search(StackLayout layout)
{
SearchBar searchBar = new SearchBar
{
Placeholder = "Xamarin.Forms Property",
};
layout.Children.Add(searchBar);
}
protected override void BuildView(StackLayout layout)
{
Search(layout);
CallDataFromDB(layout);
Device.OnPlatform(
//broken in Xamarin 1.2. Awaiting a fix
Android: () =>
{
var tbi = new ToolbarItem("Refresh", "", () =>
{
BuildView(layout);
}, 0, 0);
tbi.Order = ToolbarItemOrder.Secondary; // forces it to appear in menu on Android
if (ToolbarItems.Count == 0)
ToolbarItems.Add(tbi);
}
);
}
当我在屏幕上做手势(触摸)时,它会断开。
这正是我现在面临的错误:
程序集中缺少方法Android.Widget.SearchView::get_InputType()Mono.Android.dll,在程序集中引用Xamarin.Forms.Platform.Android.dll
有人能帮我吗?
此代码适用于我:
public class ItemsPage : ContentPage
{
ItemListView list;
SearchBar searchbar;
public ItemsPage ()
{
Title = "Items";
list = new ItemListView ();
searchbar = new SearchBar () {
Placeholder = "Search",
};
searchbar.TextChanged += (sender, e) => {
/*Work to be done at time text change*/
};
searchbar.SearchButtonPressed += (sender, e) => {
/*Work to be done at time of Search button click*/
};
var stack = new StackLayout () {
Children = {
searchbar,
list
}
};
Content = stack;
}
}
注意:
最低安卓版本:覆盖-安卓4.0.3(API 15级)
目标框架:使用最新安装的平台(API 21级)
目标Android版本:自动-API 19级
我使用了它及其工作。我设置:构建>常规>目标框架:使用最新安装的平台(4.4)构建>Android应用程序>最低Android版本:覆盖-Android 4.0.3(API 15级)构建>安卓应用程序>目标Android版本:自动-使用目标框架版本。
您在Xaml中尝试过做同样的操作吗?我正在我的应用程序中使用SearchBar,它运行得很好。下面是一些示例代码:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
Title="Results"
x:Class="MyApp.ResultPage">
<StackLayout Orientation="Vertical"
Spacing="0">
<SearchBar x:Name="SearchBar"
Placeholder="Search by name"
SearchButtonPressed="OnSearchButtonTapped"
CancelButtonColor="Gray"
TextChanged="OnSearchBarTextChanged" />
<ListView x:Name="ListView"
HasUnevenRows="true"
IsGroupingEnabled="true"
GroupDisplayBinding="{Binding Title}"
ItemTapped="DirectoryListOnItemTapped"
VerticalOptions="FillAndExpand"
SeparatorColor="Silver"
BackgroundColor="White"
IsPullToRefreshEnabled="true"
Refreshing="OnRefresButtonTapped">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding Name}"
TextColor="Black" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
<ContentPage>
我使用以下代码,我遵循MVVM设计模式在Veiw页面中写入以下代码:-
internal class NotificationsPage : ContentPage
{
#region Variables and Controlls declaration
private NotificationsViewModel _viewModel;
private SearchBar _notificationSearchBar;
#endregion Variables and Controlls declaration
#region Constructor
public NotificationsPage()
{
BindingContext = App.Locator.Notification;
_viewModel = BindingContext as NotificationsViewModel;
_notificationSearchBar = new SearchBar()
{
};
_notificationSearchBar.SetBinding(SearchBar.TextProperty,"TEXT");
_notificationSearchBar.SetBinding(SearchBar.SearchCommandProperty,"SearchCommand" );
}
在视图中Model can
#region Search Functionality
#region Commands
private RelayCommand _searchCommand;
public RelayCommand SearchCommand
{ get { return _searchCommand ?? (_searchCommand = new RelayCommand(async () =>
{
if (SearchedList.Count > 0)
{
if (!string.IsNullOrEmpty(Text))
{
List<Notification> entities = SearchedList.Where(e =>
Convert.ToString(e.NotificationSubject).ToLower().Trim().Contains(Text.ToLower().Trim())
|| Convert.ToString(e.LinkedRecordName).ToLower().Trim().Contains(Text.ToLower().Trim())
|| Convert.ToString(e.NotificationDateSent).ToLower().Trim().Contains(Text.ToLower().Trim())
|| Convert.ToString(e.NotificationContent).ToLower().Trim().Contains(Text.ToLower().Trim())).ToList();
NotificationsList = new ObservableCollection<Notification>(entities);
}
else
{
NotificationsList = SearchedList;
}
}
}
)); } }
private string _searchText = string.Empty;
public string Text // This is the text property we bind on page _notificationSearchBar.SetBinding(SearchBar.TextProperty,"TEXT");
{
get { return _searchText; }
set
{
if (_searchText != value)
{
_searchText = value;
Set(() => Text, ref _searchText, value);
if (SearchCommand.CanExecute(null))
{
SearchCommand.Execute(null);
}
}
}
}
#endregion
#endregion
SearchedList和NotificationList是绑定到列表的可观察集合,您可以将其作为类的列表以及
private ObservableCollection<Notification> _notificationsList;
public ObservableCollection<Notification> NotificationsList
{
get { return _notificationsList; }
set
{
Set(() => NotificationsList, ref _notificationsList, value);
}
}
/// <summary>
/// Holds Searched notifications
/// </summary>
private ObservableCollection<Notification> _searchedList;
public ObservableCollection<Notification> SearchedList
{
get { return _searchedList; }
set
{
Set(() => SearchedList, ref _searchedList, value);
}
}
我做了很多次。我更喜欢使用MVVM模式。我正在使用事件到命令行为将搜索命令绑定到ViewModel基本上,你搜索一些文本或数量,并操作一些你已经拥有的集合。就这么简单。我写了一篇关于这个的博客,看看这是否对你有任何帮助。谢谢:)请参考:https://adityadeshpandeadi.wordpress.com/2018/01/14/search-through-listview-items-in-xamarin-forms/