【问题标题】:C# UWP Background color not filling all the wayC# UWP 背景颜色没有一直填充
【发布时间】: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 也不起作用。背景颜色与宽度不匹配。

标签: c# windows xaml uwp


【解决方案1】:

已修复

我通过将 ContentPage 包装在单独的 Grid 中来修复它。

这是新的 MainPage.xaml

<Page
    x:Class="MediaPlayer.MainPage"
    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"
    xmlns:views="using:MediaPlayer.Views"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="0.2*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <views:SidePage Grid.Column="0" />
        <Grid Grid.Column="1">
            <views:ContentPage />
        </Grid>
    </Grid>
</Page>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-28
    • 1970-01-01
    • 1970-01-01
    • 2011-01-19
    相关资源
    最近更新 更多