【发布时间】:2023-03-18 13:55:01
【问题描述】:
在我的 MainPage.xaml 中有一个 Grid。
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.2*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<views:SidePage Grid.Column="0" />
<views:ContentPage Grid.Column="1" />
</Grid>
每个页面占用的空间量是正确的,但 ContentPage 的背景颜色与它占用的宽度不匹配。
这是 ContentPage.xaml:
<Page
x:Class="MediaPlayer.Views.ContentPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<StackPanel>
<TextBlock Text="Testing"></TextBlock>
</StackPanel>
</Page>
这是我设置背景的地方
public ContentPage()
{
InitializeComponent();
Background = new SolidColorBrush(StaticColors.ContentBackgroundColor);
}
public static readonly Color ContentBackgroundColor = Color.FromArgb(150, 0, 0, 60);
如您所见,“测试”位于白色部分。
【问题讨论】:
-
用
Grid包裹StackPanel。看起来它不尊重父母的宽度。 -
在 ContentPage 中用 Grid 包装 StackPanel 不会改变它。用 StackPanel 在 MainPage 中包装 Grid 也不起作用。背景颜色与宽度不匹配。