Xamarin Forms ios在列表视图中只显示一项



我在谷歌上搜索过,但提出的解决方案对我不起作用。我有一个这样的列表视图:

<ListView ItemsSource="{Binding Items}" RowHeight="220" ItemTapped="OnResourceTapped" IsGroupingEnabled="false" CachingStrategy="RecycleElement">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid>
<ff:CachedImage VerticalOptions="Fill" HorizontalOptions="FillAndExpand" Aspect="AspectFill" Source="{Binding preview}"></ff:CachedImage>
<StackLayout VerticalOptions="End" Orientation="Horizontal" BackgroundColor="{StaticResource darkBlue}" Opacity="0.7" HeightRequest="40" Padding="10">
<StackLayout>
<Label Text="{Binding title}" TextType="Html" VerticalTextAlignment="Center" HorizontalTextAlignment="Start" TextColor="{StaticResource white}" FontFamily="QuicksandBold"/>
<Label Text="{Binding description}" TextType="Html" VerticalTextAlignment="Center" HorizontalTextAlignment="Start" TextColor="{StaticResource yellow}" FontFamily="QuicksandRegular" LineBreakMode="TailTruncation" MaxLines="1"/>
</StackLayout>
<ff:CachedImage Source="{Binding source, Converter={StaticResource SelfCareResourceTypeImage}}" VerticalOptions="Center" HorizontalOptions="EndAndExpand" HeightRequest="30" WidthRequest="30" Margin="0,0,10,0" Aspect="AspectFit"/>
</StackLayout>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

在ViewModel中,我有:

private ObservableCollection<SelfCareResource> items;
public ObservableCollection<SelfCareResource> Items
{
get { return items; }
set
{
items = value;
}
}
...
public SelfCareViewModel()
{
string deviceLanguage = App.deviceLanguage;
Items = new ObservableCollection<SelfCareResource>();
_ = HTTPRequest.GetAllSelfCareResources(deviceLanguage, list =>
{
foreach (SelfCareResource item in list)
{
Items.Add(item);
}
});
}

服务器返回我12个资源,在Android上显示所有资源,但在iOS上只显示第一个

我做了一个关于视图模型的简单示例。如果你能确保"项目"得到所有项目,它就会显示所有项目。我在ios 15.2上进行了测试。

视图模型供您参考。

public class SelfCareViewModel : INotifyPropertyChanged
{
private ObservableCollection<SelfCareResource> items;
public event PropertyChangedEventHandler PropertyChanged;
public ObservableCollection<SelfCareResource> Items
{
get { return items; }
set
{
items = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Items"));
}
}
public SelfCareViewModel()
{
Items = new ObservableCollection<SelfCareResource>()
{
new SelfCareResource(){ title="A", description="aa", preview="YourImage", source="YourImage"},
new SelfCareResource(){ title="B", description="bb", preview="YourImage", source="YourImage"},
new SelfCareResource(){ title="C", description="cc", preview="YourImage", source="YourImage"},
};
}
}
public class SelfCareResource 
{
public string preview { get; set; }
public string title { get; set; }
public string description { get; set; }
public string source { get; set; }
}

最新更新