【发布时间】:2020-10-10 09:21:46
【问题描述】:
我目前正在尝试学习 Xamarin.forms,但我被困在课程的当前部分。
我手动创建了一个列表视图,并被告知将其转换为单独类中的“远程服务”。
当我这样做时,列表不再填充,我无法弄清楚我做错了什么。编译时出现零错误。
放置在名为“SearchServices.cs”的类中的“远程服务”代码如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
using ListExcercises.Models;
namespace ListExcercises.Services
{
public class SearchServices
{
private List<Search> _searches = new List<Search>
{
new Search
{
Id=1,
Location = "West Hollywood, CA, United States",
CheckIn = new DateTime(2016, 9, 1),
CheckOut = new DateTime(2016, 11, 1)
},
new Search
{
Id=2,
Location ="Santa Monica, CA, United States",
CheckIn = new DateTime(2016, 9, 1),
CheckOut = new DateTime(2016, 11, 1)
}
};
public IEnumerable<Search> GetRecentSearches(string filter = null)
{
if (String.IsNullOrWhiteSpace(filter))
return _searches;
return _searches.Where(s => s.Location.StartsWith(filter, StringComparison.CurrentCultureIgnoreCase));
}
public void DeleteSearch(int searchId){
_searches.Remove(_searches.Single(s => s.Id == searchId));
}
视图背后的代码如下所示:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using ListExcercises.Models;
using ListExcercises.Services;
using Xamarin.Forms;
namespace ListExcercises
{
public partial class ExcerciseListPage : ContentPage
{
private SearchServices _searchService;
private List<SearchGroup> _searchGroups;
public ExcerciseListPage()
{
_searchService = new SearchServices();
InitializeComponent();
PopulateListView(_searchService.GetRecentSearches());
}
//Create re-usable list grouping(populate list)
private void PopulateListView(IEnumerable<Search> searches)
{
_searchGroups = new List<SearchGroup>
{
new SearchGroup("Recent Searches", searches)
};
listView.ItemsSource = _searchGroups;
}
void onItemTap(object sender, Xamarin.Forms.ItemTappedEventArgs e)
{
var search = e.Item as Search;
DisplayAlert("Tapped",
"Costumer visited " + search.Location + " at " + search.CheckIn + " and left at " + search.CheckOut,
"OK");
}
void listView_ItemSelected(System.Object sender, Xamarin.Forms.SelectedItemChangedEventArgs e)
{
listView.SelectedItem = null;
}
void Delete_Clicked(System.Object sender, System.EventArgs e)
{
var search = (sender as MenuItem).CommandParameter as Search;
//Delete local
_searchGroups[0].Remove(search);
//Update backend
_searchService.DeleteSearch(search.Id);
}
void listView_Refreshing(System.Object sender, System.EventArgs e)
{
PopulateListView(_searchService.GetRecentSearches(searchBar.Text));
listView.EndRefresh();
}
void searchBar_TextChanged(Object sender, Xamarin.Forms.TextChangedEventArgs e)
{
PopulateListView(_searchService.GetRecentSearches(e.NewTextValue));
}
}
}
XAML 代码如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="ListExcercises.ExcerciseListPage">
<StackLayout Padding="0, 40, 0, 0">
<SearchBar Placeholder="Search..."
Margin="0"
HorizontalOptions="Fill"
x:Name="searchBar"
TextChanged="searchBar_TextChanged"/>
<StackLayout>
<ListView x:Name="listView" IsGroupingEnabled="True" HasUnevenRows="True"
GroupDisplayBinding="{Binding Title}"
ItemTapped="onItemTap"
ItemSelected="listView_ItemSelected"
IsPullToRefreshEnabled="True"
Refreshing="listView_Refreshing">
<ListView.GroupHeaderTemplate>
<DataTemplate>
<ViewCell>
<Label Padding="20,0,0,0" Text="{Binding Title}"
FontSize="21"
HorizontalOptions="StartAndExpand"
VerticalOptions="Center"
FontAttributes="Bold"/>
</ViewCell>
</DataTemplate>
</ListView.GroupHeaderTemplate>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.ContextActions>
<MenuItem Text="Delete" Clicked="Delete_Clicked"
IsDestructive="True" />
</ViewCell.ContextActions>
<StackLayout Padding="20, 0, 0, 0">
<Label Text="{Binding Location}" FontSize="20"/>
<StackLayout Orientation="Horizontal">
<Label Text="{Binding CheckIn, StringFormat='{0:MMM d, yyyy}'}"
TextColor="CornflowerBlue" FontSize="13"/>
<Label Text="-" TextColor="CornflowerBlue"
FontSize="13"/>
<Label Text="{Binding CheckOut, StringFormat='{0:MMM d, yyyy}'}"
TextColor="CornflowerBlue" FontSize="13"/>
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</StackLayout>
除了上面发布的代码之外,我还找到了模型,一个叫做 Search.cs,一个叫做 SearchGroup.cs。两者都放在项目的Models文件夹中。
SearchGroup 中的代码如下所示:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace ListExcercises.Models
{
public class SearchGroup : ObservableCollection<Search> {
public string Title { get; set; }
public SearchGroup(string title, IEnumerable<Search> searches)
{
Title = title;
}
}
}
最后搜索的代码是这样的:
using System;
namespace ListExcercises.Models
{
public class Search
{
public int Id { get; set; }
public string Location { get; set; }
public DateTime CheckIn { get; set; }
public DateTime CheckOut { get; set; }
public string Period
{
get
{
return String.Format("{0} - {1}",
CheckIn.ToString("MMM d, yyyy"),
CheckOut.ToString("MMM d, yyyy"));
}
}
}
}
我真的希望有人可以帮助我并给我一些指示。
TL;DR:
尝试将列表创建移动到单独的文档/类,现在我的列表视图没有填充信息。所以我可以看到大部分功能都在那里,但是所有行都是空的。
【问题讨论】:
-
您是否已验证您用于 ItemsSource 的对象实际上包含您认为的数据?
-
@Jason 不,我没有,最聪明的方法是什么?
-
使用调试器
-
@Jason 之前没有使用过断点和其他东西,但从我刚刚得到的内容来看,我只能看到它包含组的标题,而不是列表项。你知道为什么吗?如果我找到解决方案,我会在早上查看它并回来,我整个晚上都在阅读代码..
-
我建议调试
SearchGroup- 因为您没有发布该代码,所以我无法提供具体建议
标签: c# xaml xamarin.forms