Phi*_*hil 3 .net xaml xamarin maui
在我的 .Net Maui 项目中,我在 ControlTemplate 中使用 CollectionView。实现看起来像这样:
<ContentView.ControlTemplate>
<ControlTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<SearchBar
x:Name="ObjectSearchBar"
Grid.Row="0"
IsSpellCheckEnabled="False"
Keyboard="Text"
Placeholder="{TemplateBinding SearchBarPlaceholderText}"
TextChanged="ObjectSearchBar_TextChanged" />
<CollectionView
x:Name="ObjectResultView"
Grid.Row="1"
Margin="10,10,10,10"
ItemSizingStrategy="MeasureAllItems"
ItemTemplate="{StaticResource templateSelector}"
ItemsLayout="VerticalList"
ItemsSource="{TemplateBinding DataSource}"
SelectionChanged="ObjectResultView_SelectionChanged"
SelectionMode="Single">
</CollectionView>
</Grid>
</ControlTemplate>
<ContentView.ControlTemplate>
Run Code Online (Sandbox Code Playgroud)
我在 ContentPage 中使用此 ContentView。该页面包含 2 个 StackLayout,其实现如下所示:
<ContentPage.Content>
<Grid>
<StackLayout
HorizontalOptions="FillAndExpand"
IsVisible="{Binding AddNewSectionIsVisible, Converter {converter:InvertedBoolConverter}}"
Orientation="Vertical"
VerticalOptions="FillAndExpand">
<controls:ObjectSearchControl
DataSource="{Binding DataSource}"
FilterChangedCommand="{Binding FilterChangedCommand}"
HorizontalOptions="FillAndExpand"
ObjectSelectedCommand="{Binding SelectedCommand}"
SearchBarPlaceholderText="ABC"
VerticalOptions="FillAndExpand" />
</StackLayout>
<StackLayout
HorizontalOptions="FillAndExpand"
IsVisible="{Binding AddNewSectionIsVisible}"
Orientation="Vertical"
Style="{StaticResource rootStackLayout}"
VerticalOptions="Center">
....
</StackLayout>
</Grid>
</ContentPage.Content>
Run Code Online (Sandbox Code Playgroud)
当页面出现时,仅显示其中一个 StackLayout。
因此,如果我将第二行的高度设置为固定值或将 CollectionView 的高度设置为固定值,则滚动工作正常。但是,如果我使用“*”或“自动”作为第二行的高度定义,CollectionView 将不再滚动。
请问,在这种情况下有人可以帮助我吗?我还尝试使用 ScrollViews,将 Stacklayouts 或 Grid 的 VerticalOptions 设置为“FillAndExpand”,就像 MS 文档中描述的那样,但似乎对我不起作用。
所有最新的 Maui 库 (8.0.0-preview.2) 在 2023 年 3 月 29 日都遇到了同样的问题。
帮助我解决滚动问题的是VerticalOptions设置FillAndExpand
选项Fill不起作用
<CollectionView ItemsSource="{Binding .}"
VerticalOptions="FillAndExpand">
</CollectionView>
Run Code Online (Sandbox Code Playgroud)