在我的 .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>
我在 ContentPage 中使用這個 ContentView。該頁面包含 2 個 StackLayouts,它的實作如下所示:
<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>
當頁面出現時,只顯示一個 StackLayouts。
因此,如果我將第二行的高度設定為固定值或將 CollectionView 的高度設定為固定值,則滾動作業正常。但是,如果我對第二行的高度定義使用“*”或“Auto”,則 CollectionView 不再滾動。
請問,在這種情況下有人可以幫助我嗎?我還嘗試使用 ScrollViews,將 Stacklayouts 或 Grid 的 VerticalOptions 設定為“FillAndExpand”,就像 MS 檔案中描述的那樣,但似乎對我沒有任何用處。
uj5u.com熱心網友回復:
我認為布局邏輯未能實作 collectionview 應該滾動。
嘗試修復 #1
嘗試以下 2 項更改:
<ContentPage.Content>
<!-- 1) "*": make sure row 0 takes all vertical space. -->
<Grid RowDefinitions="*">
<!-- 2) remove the surrounding StackLayout, to simplify layout logic. -->
<controls:ObjectSearchControl
IsVisible="{Binding AddNewSectionIsVisible, Converter {converter:InvertedBoolConverter}}"
DataSource="{Binding DataSource}"
FilterChangedCommand="{Binding FilterChangedCommand}"
HorizontalOptions="FillAndExpand"
ObjectSelectedCommand="{Binding SelectedCommand}"
SearchBarPlaceholderText="ABC"
VerticalOptions="FillAndExpand" />
測驗 #1 是否不起作用
如果以上沒有幫助,那么請做這個測驗:
<controls:ObjectSearchControl
IsVisible="True"
... and set the other stacklayout to `IsVisible="False"`.
也就是說,作為測驗,強制該控制元件在InitializeComponent被呼叫時可見。這應該會導致 CollectionView 被正確標記為滾動。
需要此測驗的結果,才能知道下一步要采取什么方法。
嘗試修復 #2 待定
如果 #1 沒有幫助,那是因為在第一次布局頁面時,CollectionView 沒有被標記為需要滾動。
這可能是由動態屬性引起的,也可能是由在有任何內容IsVisible之前發生的布局引起的。DataSource
它是 CollectionView 中的一個微妙錯誤,在其他情況下也出現過。
待定:研究早期的問答,以確定最簡單的解決方法。
uj5u.com熱心網友回復:
通過重構布局,我找到了解決問題的方法。
我替換了所有內容并在我StackLayouts的控制元件中Grids添加了一個。ScrollView似乎我的問題的原因是復雜的布局。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/414636.html
標籤:
上一篇:由于Manage_External_Storage,應用程式在Play商店中被拒絕,即使我的應用程式沒有使用此權限
