C# 如何在另一个xaml中显示搜索栏列表的结果?
我想在另一个xml页面中显示搜索结果。如本计划所述: 搜索栏所在的页面具有以下xml代码:C# 如何在另一个xaml中显示搜索栏列表的结果?,c#,visual-studio,xamarin.forms,C#,Visual Studio,Xamarin.forms,我想在另一个xml页面中显示搜索结果。如本计划所述: 搜索栏所在的页面具有以下xml代码: <SearchBar SearchButtonPressed="SearchBar_SearchButtonPressed" 哪一行抛出异常?该行的哪个特定元素为空?在解决问题之前,您需要弄清楚这是行EmployeeListView.ItemsSource=_container.MyListCollector.Where(u=>u.nome.ToLower().Contains(e.NewTe
<SearchBar SearchButtonPressed="SearchBar_SearchButtonPressed"
哪一行抛出异常?该行的哪个特定元素为空?在解决问题之前,您需要弄清楚这是行EmployeeListView.ItemsSource=_container.MyListCollector.Where(u=>u.nome.ToLower().Contains(e.NewTextValue.ToLower())| u.cognome.ToLower().Contains(e.NewTextValue.ToLower());这一行中有十几个东西可能是空的-哪一个是空的?我不知道我没有报告主题,但是整行你需要使用调试器来找出它。我们没办法帮你。如果您不知道如何使用调试器(您应该学习),您可以在执行该行之前添加代码来测试每个元素的null值。
public partial class FriendsPage : ContentPage
{
public FriendsPage()
{
InitializeComponent();
Xamarin.Forms.NavigationPage.SetHasNavigationBar(this, false);
On<Xamarin.Forms.PlatformConfiguration.iOS>().SetUseSafeArea(true);
}
private void SearchBar_SearchButtonPressed(object sender, EventArgs e)
{
var search = sender as SearchBar;
this.Navigation.PushModalAsync(new SearchBarFriends(search.Text), true);
}
}
public partial class SearchBarFriends : ContentPage
{
public SearchBarFriends()
{
InitializeComponent();
}
public SearchBarFriends(String key)
{
InitializeComponent();
searchBar.Text = key;
Xamarin.Forms.NavigationPage.SetHasNavigationBar(this, false);
On<Xamarin.Forms.PlatformConfiguration.iOS>().SetUseSafeArea(true);
BindingContext = new MyListViewModelUser();
}
async void Handle_ItemTapped(object sender, Xamarin.Forms.ItemTappedEventArgs e)
{
DbConnectionClass db = new DbConnectionClass();
User u = e.Item as User;
int id = db.QueryId("SELECT id FROM thewishlist.user WHERE nome='" + u.nome + "'OR cognome='" + u.cognome +"'");
var friendsCorrenteJson = JsonConvert.SerializeObject(u);
App.Current.Properties["friends"] = friendsCorrenteJson;
await App.Current.SavePropertiesAsync();
await Navigation.PushAsync(new ProfileFriend(u));
db.CloseConnection();
}
//clicked on searchbar
void Handle_TextChanged(object sender, TextChangedEventArgs e)
{
var search = sender as SearchBar;
var _container = BindingContext as MyListViewModelUser;
EmployeeListView.BeginRefresh();
if (string.IsNullOrWhiteSpace(e.NewTextValue))
EmployeeListView.ItemsSource = _container.MyListCollector;
else
{
EmployeeListView.ItemsSource = _container.MyListCollector.Where(
u => u.nome.ToLower().Contains(e.NewTextValue.ToLower()) || u.cognome.ToLower().Contains(e.NewTextValue.ToLower()));
}
EmployeeListView.EndRefresh();
}
}
<SearchBar x:Name="searchBar" Placeholder="Friends..." FontAttributes="Italic" PlaceholderColor="Silver" TextChanged="Handle_TextChanged" BackgroundColor="#FFFC79" HorizontalOptions="Start" VerticalOptions="Start" CancelButtonColor="Black"/>
<ListView ItemsSource="{Binding MyListCollector}" IsPullToRefreshEnabled="true" x:Name="EmployeeListView" HasUnevenRows="true" ItemTapped="Handle_ItemTapped" >
<ListView.ItemTemplate>
EmployeeListView.ItemsSource = _container.MyListCollector.Where(
u => u.nome.ToLower().Contains(e.NewTextValue.ToLower()) || u.cognome.ToLower().Contains(e.NewTextValue.ToLower()));