我使用以下代码使用xamarin表单创建搜索栏。 但它在这一点上打破了。任何人都可以帮助我。
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);
}
);
}
当我在屏幕上做出手势(触摸)时,它会中断。
这是我现在面临的确切错误。我正在使用Xamarin 程序集Mono.Android.dll中缺少方法Android.Widget.SearchView :: get_InputType(),在程序集Xamarin.Forms.Platform.Android.dll中引用
答案 0 :(得分:1)
此代码适用于我:
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;
}
}
注意:
最低Android版本:覆盖 - Android 4.0.3(API级别15)
目标框架:使用最新安装的平台(API Level 21)
目标Android版:自动 - API等级19
答案 1 :(得分:0)
我用它和它的工作。 我设置:Build>一般>目标框架:使用最新安装的平台(4.4)Build> Android应用程序>最低Android版本:覆盖 - Android 4.0.3(API级别15)构建> Android应用程序>目标Android版本:自动 - 使用目标框架版本。
答案 2 :(得分:0)
你有没有试过在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>
答案 3 :(得分:0)
我正在使用以下代码,我遵循MVVM设计模式 在Veiw Page中写下波纹管代码: -
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" );
}
在viewModel中可以
#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);
}
}
答案 4 :(得分:0)
我做了很多次。 我更喜欢使用MVVM模式。 我正在使用事件来命令行为将搜索命令绑定到ViewModel 基本上你搜索一些文本或数量,并对你已经拥有的一些集合进行操作。就如此容易。 我已经写了blog这个,看看这对你有什么帮助。 谢谢 :) 请参考: https://adityadeshpandeadi.wordpress.com/2018/01/14/search-through-listview-items-in-xamarin-forms/