我复制了您的代码,只需为网格添加
Grid.RowSpan="3"
即可。然后我就得到了你想要的效果,没有LayoutChanged
和Appearing
。
完整的xaml:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MauiApp7.MainPage" Shell.NavBarIsVisible="False">
<Grid BackgroundColor="Pink">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80" />
<ColumnDefinition Width="80"/>
<ColumnDefinition Width="80"/>
<ColumnDefinition Width="80"/>
<ColumnDefinition Width="80"/>
<ColumnDefinition Width="80"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="80"/>
<RowDefinition Height="80"/>
<RowDefinition Height="80"/>
</Grid.RowDefinitions>
<Grid x:Name="gridLayoutBox"
BackgroundColor="Blue"
IsClippedToBounds="True"
Grid.ColumnSpan="6"
Grid.RowSpan="3" >
</Grid>
</Grid>
</ContentPage>
结果图像:
另外,如果想强制横向,可以将以下代码全部放在Page的OnHandlerChanged方法或者OnAppearing方法中。
#if ANDROID
Microsoft.Maui.ApplicationModel.Platform.CurrentActivity.RequestedOrientation = Android.Content.PM.ScreenOrientation.Landscape;
#endif
在人像模式下,蓝色网格会被剪掉,因为它的宽度大于屏幕的宽度。