英文:
Unable to Scroll to Bottom of List in a MAUI Application
问题
问题:我正在开发一个移动应用程序,用于跟踪用户每天执行的程序。
问题:我遇到一个问题,当点击用户的个人资料并查看其程序历史时,如果用户已完成大量的程序,我无法滚动到列表的底部。
以下是我的实现:
-> 视图(程序历史):
通过第一种解决方案修改的代码(但无法滚动到列表底部)
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ProjetSport.Views.ExerciceActivitiesView"
xmlns:vm="clr-namespace:ProjetSport.ViewModels"
Title="ExerciceActivitiesView">
<ContentPage.BindingContext>
<vm:ActiviteExerciceViewModel></vm:ActiviteExerciceViewModel>
</ContentPage.BindingContext>
<ContentPage.BackgroundColor>
<OnPlatform x:TypeArguments="Color">
<On Platform="iOS" Value="#FEFAE2" />
<On Platform="Android" Value="#FEFAE2" />
<On Platform="UWP" Value="#FEFAE2" />
</OnPlatform>
</ContentPage.BackgroundColor>
<Grid RowDefinitions="Auto,*">
<VerticalStackLayout Grid.Row="0">
<HorizontalStackLayout HorizontalOptions="Center" Margin="0,20,0,20">
<Label Text="您已完成:" ></Label>
<Label Text="{Binding Avancee}" FontAttributes="Bold"></Label>
<Label Text="%" FontAttributes="Bold"></Label>
<Label Text=" 选定的计划" ></Label>
</HorizontalStackLayout>
<HorizontalStackLayout HorizontalOptions="Center">
<Border Stroke="#000000"
StrokeThickness="5"
StrokeShape="RoundRectangle 50,50,50,50"
BackgroundColor="#DFDBC6"
HorizontalOptions="Center">
<Button Text="显示练习" Clicked="Button_Clicked" HorizontalOptions="Center" BackgroundColor="#FEF8BF" TextColor="#000000"></Button>
</Border>
<Border Stroke="#000000"
StrokeThickness="5"
StrokeShape="RoundRectangle 50,50,50,50"
BackgroundColor="#DFDBC6"
HorizontalOptions="Center">
<Button Text="减少" Clicked="Button_Clicked_1" HorizontalOptions="Center" BackgroundColor="#FEF8BF" TextColor="#000000"></Button>
</Border>
</HorizontalStackLayout>
</VerticalStackLayout>
<CollectionView Grid.Row="1"
WidthRequest="350"
x:Name="CollectionView"
IsVisible="true"
ItemTemplate="{StaticResource Exercices}"
BackgroundColor="#FEFAE2"
ItemsSource="{Binding SelectedProgramActivity}"
/>
</Grid>
</ContentPage>
-> 代码后台:
public partial class ExerciceActivitiesView : ContentPage
{
public ExerciceActivitiesView()
{
InitializeComponent();
}
private void Button_Clicked(object sender, EventArgs e)
{
CollectionView.IsVisible = true;
}
private void Button_Clicked_1(object sender, EventArgs e)
{
CollectionView.IsVisible = false;
}
}
-> 数据模板
<DataTemplate x:Key="Activite">
<Border Stroke="#000000" StrokeThickness="3" BackgroundColor="#F2EED5" StrokeShape="RoundRectangle 10, 20, 20, 10" Margin="0,10,5,20">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<HorizontalStackLayout Margin="50,20,0,20">
<Label Text="{Binding Name}"
Grid.Column="1"
HorizontalTextAlignment="Center"
FontSize="Small"
Margin="-10,0,0,0"
TextColor="#100905"
></Label>
</HorizontalStackLayout>
<HorizontalStackLayout Margin="240,15,0,0">
<Label Text="{Binding Date, StringFormat='{0:dd/MM/yyyy}'}"
Grid.Column="2"
HorizontalTextAlignment="Center"
FontSize="Small"
Margin="-10,0,0,0"
TextColor="#100905"
></Label>
</HorizontalStackLayout>
</Grid>
</Border>
</DataTemplate>
我将不胜感激地接受有关如何解决此滚动问题的任何指导。提前感谢!
英文:
Context: I'm working on a mobile application that tracks the programs performed by a user each day.
Problem: I'm experiencing an issue where, upon clicking on a user's profile and viewing their program history, I cannot scroll down to the bottom of the list if the user has completed a large number of programs.
Here is my implementation:
-> View (Program History):
Code modified by the first solution (but impossible to scroll to the bottom of the list)
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ProjetSport.Views.ExerciceActivitiesView"
xmlns:vm="clr-namespace:ProjetSport.ViewModels"
Title="ExerciceActivitiesView">
<ContentPage.BindingContext>
<vm:ActiviteExerciceViewModel></vm:ActiviteExerciceViewModel>
</ContentPage.BindingContext>
<ContentPage.BackgroundColor>
<OnPlatform x:TypeArguments="Color">
<On Platform="iOS" Value="#FEFAE2" />
<On Platform="Android" Value="#FEFAE2" />
<On Platform="UWP" Value="#FEFAE2" />
</OnPlatform>
</ContentPage.BackgroundColor>
<Grid RowDefinitions="Auto,*">
<VerticalStackLayout Grid.Row="0">
<HorizontalStackLayout HorizontalOptions="Center" Margin="0,20,0,20">
<Label Text="Vous avez effectué : "></Label>
<Label Text="{Binding Avancee}" FontAttributes="Bold"></Label>
<Label Text="%" FontAttributes="Bold"></Label>
<Label Text=" du programme selectionné"></Label>
</HorizontalStackLayout>
<HorizontalStackLayout HorizontalOptions="Center">
<Border Stroke="#000000"
StrokeThickness="5"
StrokeShape="RoundRectangle 50,50,50,50"
BackgroundColor="#DFDBC6"
HorizontalOptions="Center">
<Button Text="Afficher les exercices" Clicked="Button_Clicked" HorizontalOptions="Center" BackgroundColor="#FEF8BF" TextColor="#000000"></Button>
</Border>
<Border Stroke="#000000"
StrokeThickness="5"
StrokeShape="RoundRectangle 50,50,50,50"
BackgroundColor="#DFDBC6"
HorizontalOptions="Center">
<Button Text="Réduire" Clicked="Button_Clicked_1" HorizontalOptions="Center" BackgroundColor="#FEF8BF" TextColor="#000000"></Button>
</Border>
</HorizontalStackLayout>
</VerticalStackLayout>
<CollectionView Grid.Row="1"
WidthRequest="350"
x:Name="CollectionView"
IsVisible="true"
ItemTemplate="{StaticResource Exercices}"
BackgroundColor="#FEFAE2"
ItemsSource="{Binding SelectedProgramActivity}"
/>
</Grid>
</ContentPage>
-> Code-behind :
public partial class ExerciceActivitiesView : ContentPage
{
public ExerciceActivitiesView()
{
InitializeComponent();
}
private void Button_Clicked(object sender, EventArgs e)
{
CollectionView.IsVisible = true;
}
private void Button_Clicked_1(object sender, EventArgs e)
{
CollectionView.IsVisible = false;
}
}
-> DataTemplate
<DataTemplate x:Key="Activite">
<Border Stroke="#000000" StrokeThickness="3" BackgroundColor="#F2EED5" StrokeShape="RoundRectangle 10, 20, 20, 10" Margin="0,10,5,20">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<HorizontalStackLayout Margin="50,20,0,20">
<Label Text="{Binding Name}"
Grid.Column="1"
HorizontalTextAlignment="Center"
FontSize="Small"
Margin="-10,0,0,0"
TextColor="#100905"
></Label>
</HorizontalStackLayout>
<HorizontalStackLayout Margin="240,15,0,0">
<Label Text="{Binding Date, StringFormat='{0:dd/MM/yyyy}'}"
Grid.Column="2"
HorizontalTextAlignment="Center"
FontSize="Small"
Margin="-10,0,0,0"
TextColor="#100905"
/>
</HorizontalStackLayout>
</Grid>
</Border>
</DataTemplate>
I would appreciate any guidance on how to fix this scrolling issue. Thanks in advance!
答案1
得分: 1
这应该有所帮助,如果Maui在VerticalStackLayout中的CollectionView出现问题。将CollectionView移动到它自己的Grid行,并将其设置为占用所有剩余空间 *
:
<Grid RowDefinitions="Auto,*">
<VerticalStackLayout Grid.Row="0">
... 除了CollectionView之外的所有XAML ...
</VerticalStackLayout>
<CollectionView Grid.Row="1" ...>
...
</CollectionView>
</Grid>
英文:
This should help, if Maui is having some issue with CollectionView inside VerticalStackLayout. Move CollectionView into its own Grid Row, and give it all remaining space *
:
<Grid RowDefinitions="Auto,*">
<VerticalStackLayout Grid.Row="0">
... all your xaml EXCEPT CollectionView ...
</VerticalStackLayout>
<CollectionView Grid.Row="1" ...>
...
</CollectionView>
</Grid>
答案2
得分: 1
您也可以尝试以下方法,以允许滚动您的数据,如果数据量很大
<ContentPage.BackgroundColor>
<OnPlatform x:TypeArguments="Color">
<On Platform="iOS" Value="#FEFAE2" />
<On Platform="Android" Value="#FEFAE2" />
<On Platform="UWP" Value="#FEFAE2" />
</OnPlatform>
</ContentPage.BackgroundColor>
<ScrollView>
<VerticalStackLayout>
<!-- 项目的代码 -->
</VerticalStackLayout>
</ScrollView>
</ContentPage>
英文:
You can also try this to allow scrolling of your data if you have a lot of it
<ContentPage.BackgroundColor>
<OnPlatform x:TypeArguments="Color">
<On Platform="iOS" Value="#FEFAE2" />
<On Platform="Android" Value="#FEFAE2" />
<On Platform="UWP" Value="#FEFAE2" />
</OnPlatform>
</ContentPage.BackgroundColor>
<ScrollView>
<VerticalStackLayout>
<!-- The code of your project -->
</VerticalStackLayout>
</ScrollView>
</ContentPage>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论