C# 工具栏在使用Xamarin.Forms的Android上不起作用
我使用以下代码使用Xamarin.Forms创建了一个搜索栏 但它在这一点上破裂了C# 工具栏在使用Xamarin.Forms的Android上不起作用,c#,android,xamarin,xamarin.forms,toolbar,C#,Android,Xamarin,Xamarin.forms,Toolbar,我使用以下代码使用Xamarin.Forms创建了一个搜索栏 但它在这一点上破裂了 protected void Search(StackLayout layout) { SearchBar searchBar = new SearchBar { Placeholder = "Xamarin.Forms Property", }; layout.Children.Add(searchBar); } protected override void
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
有人能帮我做这个吗?我用过这个和它的工作原理。
我设置:构建>常规>目标框架:使用最新安装的平台(4.4)构建>安卓应用程序>最低安卓版本:覆盖-安卓4.0.3(API级别15)构建>安卓应用程序>目标安卓版本:自动-使用目标框架版本。您在Xaml中尝试过同样的操作吗?我在我的应用程序中使用了一个搜索栏,它工作得很好。下面是一些示例代码:
<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>
此代码适用于我:
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
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" );
}
在视图中,模型可以
#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
#区域搜索功能
#区域命令
专用中继命令_searchCommand;
公共中继命令SearchCommand
{get{return\u searchCommand???(\u searchCommand=new RelayCommand(async()=>
{
如果(SearchedList.Count>0)
{
如果(!string.IsNullOrEmpty(Text))
{
列表实体=搜索列表。其中(e=>
Convert.ToString(例如NotificationSubject.ToLower().Trim().Contains(Text.ToLower().Trim())
||Convert.ToString(例如LinkedRecordName).ToLower().Trim()包含(Text.ToLower().Trim())
||Convert.ToString(例如NotificationDateSent.ToLower().Trim().Contains(Text.ToLower().Trim())
||Convert.ToString(例如NotificationContent.ToLower().Trim().Contains(Text.ToLower().Trim()).ToList();
NotificationsList=新的ObservableCollection(实体);
}
其他的
{
NotificationsList=搜索列表;
}
}
}
)); } }
私有字符串_searchText=string.Empty;
公共字符串Text//这是我们在页面_notificationSearchBar.SetBinding(SearchBar.TextProperty,“Text”)上绑定的文本属性;
{
获取{return\u searchText;}
设置
{
如果(_searchText!=值)
{
_搜索文本=值;
设置(()=>Text,ref _searchText,value);
if(SearchCommand.CanExecute(null))
{
SearchCommand.Execute(null);
}
}
}
}
#端区
#端区
其中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);
}
}
private observeCollection\u notifications列表;
公共观察收集通知列表
{
获取{return\u notificationsList;}
设置
{
设置(()=>NotificationsList,ref _NotificationsList,value);
}
}
///
///保存搜索到的通知
///
私有可观察收集_searchedList;
公共可观测集合搜索列表
{
获取{return\u searchedList;}
设置
{
设置(()=>SearchedList,ref _SearchedList,value);
}
}
我经常这样做。
我更喜欢使用MVVM模式。
我正在使用事件命令行为将搜索命令绑定到ViewModel
基本上,你搜索一些文本或数量,并对一些你已经拥有的集合进行操作。就这么简单。
我写了一篇关于这方面的文章,看看这对你有什么帮助。
谢谢:)
请参阅:
请告诉我当它(请求)中断时您得到的异常。请发布完整的类未处理异常。未创建对象的实例。如上所述,发布完整的类。我的怀疑是,您试图在页面调用InitializeComponents之前使用布局。