无法滚动到 MAUI 应用程序中列表的底部

huangapple go评论60阅读模式
英文:

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)

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;
&lt;ContentPage xmlns=&quot;http://schemas.microsoft.com/dotnet/2021/maui&quot;
xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
x:Class=&quot;ProjetSport.Views.ExerciceActivitiesView&quot;
xmlns:vm=&quot;clr-namespace:ProjetSport.ViewModels&quot;
Title=&quot;ExerciceActivitiesView&quot;&gt;
&lt;ContentPage.BindingContext&gt;
&lt;vm:ActiviteExerciceViewModel&gt;&lt;/vm:ActiviteExerciceViewModel&gt;
&lt;/ContentPage.BindingContext&gt;
&lt;ContentPage.BackgroundColor&gt;
&lt;OnPlatform x:TypeArguments=&quot;Color&quot;&gt;
&lt;On Platform=&quot;iOS&quot; Value=&quot;#FEFAE2&quot; /&gt;
&lt;On Platform=&quot;Android&quot; Value=&quot;#FEFAE2&quot; /&gt;
&lt;On Platform=&quot;UWP&quot; Value=&quot;#FEFAE2&quot; /&gt;
&lt;/OnPlatform&gt;
&lt;/ContentPage.BackgroundColor&gt;
&lt;Grid RowDefinitions=&quot;Auto,*&quot;&gt;
&lt;VerticalStackLayout Grid.Row=&quot;0&quot;&gt;
&lt;HorizontalStackLayout HorizontalOptions=&quot;Center&quot; Margin=&quot;0,20,0,20&quot;&gt;
&lt;Label Text=&quot;Vous avez effectu&#233; : &quot;&gt;&lt;/Label&gt;
&lt;Label Text=&quot;{Binding Avancee}&quot; FontAttributes=&quot;Bold&quot;&gt;&lt;/Label&gt;
&lt;Label Text=&quot;%&quot; FontAttributes=&quot;Bold&quot;&gt;&lt;/Label&gt;
&lt;Label Text=&quot; du programme selectionn&#233;&quot;&gt;&lt;/Label&gt;
&lt;/HorizontalStackLayout&gt;
&lt;HorizontalStackLayout HorizontalOptions=&quot;Center&quot;&gt;
&lt;Border Stroke=&quot;#000000&quot;
StrokeThickness=&quot;5&quot;
StrokeShape=&quot;RoundRectangle 50,50,50,50&quot;
BackgroundColor=&quot;#DFDBC6&quot;
HorizontalOptions=&quot;Center&quot;&gt;
&lt;Button Text=&quot;Afficher les exercices&quot; Clicked=&quot;Button_Clicked&quot; HorizontalOptions=&quot;Center&quot; BackgroundColor=&quot;#FEF8BF&quot; TextColor=&quot;#000000&quot;&gt;&lt;/Button&gt;
&lt;/Border&gt;
&lt;Border Stroke=&quot;#000000&quot;
StrokeThickness=&quot;5&quot;
StrokeShape=&quot;RoundRectangle 50,50,50,50&quot;
BackgroundColor=&quot;#DFDBC6&quot;
HorizontalOptions=&quot;Center&quot;&gt;
&lt;Button Text=&quot;R&#233;duire&quot; Clicked=&quot;Button_Clicked_1&quot; HorizontalOptions=&quot;Center&quot; BackgroundColor=&quot;#FEF8BF&quot; TextColor=&quot;#000000&quot;&gt;&lt;/Button&gt;
&lt;/Border&gt;
&lt;/HorizontalStackLayout&gt;
&lt;/VerticalStackLayout&gt;
&lt;CollectionView Grid.Row=&quot;1&quot;
WidthRequest=&quot;350&quot;
x:Name=&quot;CollectionView&quot;
IsVisible=&quot;true&quot;
ItemTemplate=&quot;{StaticResource Exercices}&quot;
BackgroundColor=&quot;#FEFAE2&quot;
ItemsSource=&quot;{Binding SelectedProgramActivity}&quot;
/&gt;
&lt;/Grid&gt;
&lt;/ContentPage&gt;

-> 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


&lt;DataTemplate x:Key=&quot;Activite&quot;&gt;
&lt;Border Stroke=&quot;#000000&quot; StrokeThickness=&quot;3&quot; BackgroundColor=&quot;#F2EED5&quot; StrokeShape=&quot;RoundRectangle 10, 20, 20, 10&quot; Margin=&quot;0,10,5,20&quot;&gt;
&lt;Grid&gt;
&lt;Grid.ColumnDefinitions&gt;
&lt;ColumnDefinition Width=&quot;*&quot;&gt;&lt;/ColumnDefinition&gt;
&lt;ColumnDefinition Width=&quot;*&quot;&gt;&lt;/ColumnDefinition&gt;
&lt;/Grid.ColumnDefinitions&gt;
&lt;HorizontalStackLayout Margin=&quot;50,20,0,20&quot;&gt;
&lt;Label Text=&quot;{Binding Name}&quot;
Grid.Column=&quot;1&quot;
HorizontalTextAlignment=&quot;Center&quot;
FontSize=&quot;Small&quot;
Margin=&quot;-10,0,0,0&quot;
TextColor=&quot;#100905&quot;
&gt;&lt;/Label&gt;
&lt;/HorizontalStackLayout&gt;
&lt;HorizontalStackLayout Margin=&quot;240,15,0,0&quot;&gt;
&lt;Label Text=&quot;{Binding Date, StringFormat=&#39;{0:dd/MM/yyyy}&#39;}&quot;
Grid.Column=&quot;2&quot;
HorizontalTextAlignment=&quot;Center&quot;
FontSize=&quot;Small&quot;
Margin=&quot;-10,0,0,0&quot;
TextColor=&quot;#100905&quot;
/&gt;
&lt;/HorizontalStackLayout&gt;
&lt;/Grid&gt;
&lt;/Border&gt;
&lt;/DataTemplate&gt;

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 *:

&lt;Grid RowDefinitions=&quot;Auto,*&quot;&gt;
&lt;VerticalStackLayout Grid.Row=&quot;0&quot;&gt;
... all your xaml EXCEPT CollectionView ...
&lt;/VerticalStackLayout&gt;
&lt;CollectionView Grid.Row=&quot;1&quot; ...&gt;
...
&lt;/CollectionView&gt;
&lt;/Grid&gt;

答案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


&lt;ContentPage.BackgroundColor&gt;
&lt;OnPlatform x:TypeArguments=&quot;Color&quot;&gt;
&lt;On Platform=&quot;iOS&quot; Value=&quot;#FEFAE2&quot; /&gt;
&lt;On Platform=&quot;Android&quot; Value=&quot;#FEFAE2&quot; /&gt;
&lt;On Platform=&quot;UWP&quot; Value=&quot;#FEFAE2&quot; /&gt;
&lt;/OnPlatform&gt;
&lt;/ContentPage.BackgroundColor&gt;
&lt;ScrollView&gt;
&lt;VerticalStackLayout&gt;
&lt;!-- The code of your project  --&gt;
&lt;/VerticalStackLayout&gt;
&lt;/ScrollView&gt;
&lt;/ContentPage&gt;

huangapple
  • 本文由 发表于 2023年5月25日 01:32:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76326087.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定