【发布时间】:2018-10-23 21:58:44
【问题描述】:
我正在开发我的第一个 Xamarin 应用程序。我想用分组制作一个列表视图,它成功了。我唯一的问题是它不会滚动。我在另一个页面上的另一个列表视图滚动没有问题,但我的分组列表视图不会这样做。我在 Android 模拟器和我的 Android 手机上都试过这个(我没有 macbook 或在 iOS 上测试的东西),它不会在其中任何一个上滚动。我查了一下,但是很多人把列表视图放在滚动视图中,而我没有这样做。
这是我的 XAML 代码:
<StackLayout Margin="10" Orientation="Vertical">
<Label Text="{Binding Title}" FontAttributes="Bold"
FontSize="20" HorizontalOptions="CenterAndExpand" />
<Label Text="{Binding Subtitle}" FontAttributes="Italic"
FontSize="15" HorizontalOptions="CenterAndExpand" />
<ListView HasUnevenRows="True" SeparatorColor="Accent"
VerticalOptions="FillAndExpand" IsEnabled="False"
IsGroupingEnabled="True" GroupDisplayBinding="{Binding Key}"
ItemsSource="{Binding ScansGroup}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Spacing="4">
<StackLayout Orientation="Horizontal" Margin="10,7,10,1">
<Label Text="{Binding Location, StringFormat='{0}'}"
FontAttributes="Bold" FontSize="16" />
<Label Text="{Binding DateTime, StringFormat='{0:dd/MM/y HH:mm}'}"
HorizontalOptions="EndAndExpand" />
</StackLayout>
<StackLayout Orientation="Horizontal">
<Label Text="{Binding ElapsedTimeOnLocation}"
HorizontalOptions="Start"
Margin="10,0,10,7" />
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
我把它放在一个 MVVM 结构中,我使用从这里得到的 MVVM 助手进行分组 https://channel9.msdn.com/Shows/XamarinShow/The-Xamarin-Show-12-MVVM-Helpers 。 我用于分组的代码如下:
public ObservableRangeCollection<Grouping<string, ScanViewModel>> ScansGroup { get; } =
new ObservableRangeCollection<Grouping<string, ScanViewModel>>();
void Group()
{
var grouped = from scan in Scans
group scan by scan.Day into scanGroup
select new Grouping<string, ScanViewModel>(scanGroup.Key, scanGroup);
ScansGroup.ReplaceRange(grouped);
}
分组显示完美,列表也显示。唯一的问题是 我无法滚动。有人可以帮我吗?
【问题讨论】:
-
@DiegoRafaelSouza 显然我犯了一个星期一早上的错误并粘贴了错误的代码。感谢您指出。这个问题现在有了正确的代码。
-
我很清楚这一点。当并非所有项目都适合屏幕时,我希望能够滚动,但事实并非如此。
标签: c# xaml listview scroll xamarin.forms