XAML .NET MAUI应用程序中的渐变页面背景

m0rkklqb  于 2023-04-27  发布在  .NET
关注(0)|答案(1)|浏览(182)

如何在.NET MAUI应用程序中将页面设置为具有线性背景?
我尝试在Colors.xaml中定义一个LinearGradientBrush,然后将其分配为StaticResource(见下文),但这似乎不起作用。
Colors.xaml中,我有:

<LinearGradientBrush x:Key="PageGradientBackground" EndPoint="1,1">
     <GradientStop
         Color="{StaticResource UIOffWhite}"
         Offset="0.1"/>
     <GradientStop
         Color="{StaticResource UILightGray}"
         Offset="0.6"/>
     <GradientStop
         Color="{StaticResource UIMidGray}"
         Offset="1.0"/>
</LinearGradientBrush>

然后像这样使用它:

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             BackgroundColor="{StaticResource PageGradientBackground}">
    <!-- ... -->
</ContentPage>

我也尝试过内联定义它,但也不起作用。实际上这是不允许的:

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
    <ContentPage.BackgroundColor>
        <LinearGradientBrush>
        </LinearGradientBrush>
    </ContentPage.BackgroundColor>
    <!-- ... -->
</ContentPage>

有什么想法,我可以有一个梯度背景的ContentPage

vbkedwbf

vbkedwbf1#

你可以像下面的代码一样使用LinearGradientBrush。你可以把LinearGradientBrush放在<ContentPage.Background>中,它可以很好地工作。

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiApp7.MainPage">
    <ContentPage.Background>
        
            <LinearGradientBrush EndPoint="1,0">
                <GradientStop Color="Yellow"
                          Offset="0.1" />
                <GradientStop Color="Green"
                          Offset="1.0" />
            </LinearGradientBrush>

    </ContentPage.Background>
</ContentPage>

有关更多信息,请参阅本文Linear gradient brushes

相关问题